首页 > 解决方案 > java.lang.ClassCastException 在休眠中调用 session.getUniqueResult 或 session.get

问题描述

Class cast exception打电话时session.getUniqueResult。我尝试过session.get但以同样的错误结束。

从日志

java.lang.ClassCastException: com.sample.functional.role.model.Role cannot be cast to com.sample.functional.role.model.Role
    at com.sample.functional.role.dao.RoleDaoImpl.getRole(RoleDaoImpl.java:84) ~[classes/:na]
    at com.sample.functional.role.dao.RoleDaoImpl$$FastClassBySpringCGLIB$$8ae202bf.invoke(<generat

我不明白为什么休眠会给出上述错误。

角色休眠映射

@Entity
@Table(name = "roles")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "roleid")
    private String roleID;


    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb",name="privileges")
    private List<Privilege>  privileges;


    @Column(name="rolename")
    private String rolename;

    @Column(name = "emailid")
    private String emailID;

    @Column(name = "isadmin")
    private boolean isAdmin;

    @Column(name = "status")
    private boolean status;




    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getRoleID() {
        return roleID;
    }

    public void setRoleID(String roleID) {
        this.roleID = roleID;
    }

    public String getRolename() {
        return rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }

    public String getEmailID() {
        return emailID;
    }

    public void setEmailID(String emailID) {
        this.emailID = emailID;
    }

    public boolean isAdmin() {
        return isAdmin;
    }

    public void setAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public Long getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Long createdDate) {
        this.createdDate = createdDate;
    }

    public Long getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Long updatedDate) {
        this.updatedDate = updatedDate;
    }

    public boolean isIsrecActive() {
        return isrecActive;
    }

    public void setIsrecActive(boolean isrecActive) {
        this.isrecActive = isrecActive;
    }

    public List<Privilege> getPrivileges() {
        return privileges;
    }

    public void setPrivileges(List<Privilege> privileges) {
        this.privileges = privileges;
    }



}

获取角色方法:

public Role getRole(String roleId) {
        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
            String queryString = "from Role where roleID = :roleID "/* +"and isrecactive = 'true'" */;
            Query query = session.createQuery(queryString);
            query.setParameter("roleID", roleId);
            Role role =(Role) query.uniqueResult();
            session.close();
            return role;
        } catch (HibernateException ex) {
            log.error("Exception {} " + ex);
            throw ex;
        }
    }

标签: javahibernatejava-8

解决方案


推荐阅读