调用session.getUniqueResult或session.get时处于休眠状态的java.lang.ClassCastException

时间:2019-03-24 10:26:17

标签: java hibernate java-8

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;
    }



}

getRole方法:

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;
        }
    }

0 个答案:

没有答案