java.lang.IllegalStateException:在查询中找不到超类型

时间:2013-03-18 16:51:54

标签: java hibernate jpa jpa-2.0

我有以下

@Entity
@Table(name = "PROJECTS")
public class Project implements Serializable {

  @Id
  private Integer SlNo;

  @Id
  private Long projectNo;

  private Date projectDate;
}

和DAO课程

CriteriaBuilder cb = entityManager.getCriteriaBuilder();

CriteriaQuery<Long> countQ = cb.createQuery(Long.class);
Root<Project> empCount = countQ.from(Project.class);
countQ.select(cb.count(empCount));

TypedQuery<Long> countquery = entityManager.createQuery(countQ);// error in this line

我在上面的行中遇到异常java.lang.IllegalStateException: No supertype found。如何解决或解决此问题?看起来有一个bug,对此有什么解决方法吗?

我正在使用Hibernate 4.1.0.Final

2 个答案:

答案 0 :(得分:0)

我已在Entity类中使用@EmbeddedId并在Primary Key类中使用@Embeddable解决了此问题。

@Entity
@Table(name = "PROJECTS")
public class Project {
@Column(name = "SL_NO" , insertable = false, updatable = false)
private Integer SlNo;
@Column(name = "PROJECT_NO" , insertable = false, updatable = false)
private Long projectNo;
private Date projectDate;

@EmbeddedId
ProjectPK projectPK;

和主键类

@Embeddable
public class ProjectPK implements Serializable {
@Column(name = "SL_NO")
private Integer SlNo;
@Column(name = "PROJECT_NO")
private Long projectNo; 

//with hashCode and equals implementation

答案 1 :(得分:0)

对于使用@EmbeddedId的情况,这是我的解决方案。这段代码我在一个类中编写,在Entity类中。

  • Class MyEntity - 这是我的表的实际实体类。 &#34; OtherFields&#34;是那些不属于主键的字段。
  • Class MyEntityPrimaryKeys - 这是为我的复合键创建的类,它为我的&#34; MyEntity&#34;类。 ROLLNO和AGE一起成为主键。

MyEntity.java

@Entity
@Table(name = "myTable")
public class MyEntity extends GenericPersistableEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId 
    MyEntityPrimaryKeys id;//Composite Primary key

    //Composite fields can be declared here for getter and setters
    @Column(name = "ROLLNO")
    private Long RollNo;

    //Composite fields can be declared here for getter and setters
    @Column(name = "AGE")
    private Long age;

    @Column(name = "OtherFields"
    private Long OtherFields;

    //getter and setters comes here
}



@Embeddable
 class MyEntityPrimaryKeys  implements Serializable{

    private static final long serialVersionUID = 1L;

    @Column(name = "ROLLNO")
    Long RollNo;

    @Column(name = "AGE")
    Long age;

    @Override
    public int hashCode() {
        HashCodeBuilder hcb = new HashCodeBuilder();
        hcb.append(RollNo);
        hcb.append(age);
        return hcb.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof MyEntityPrimaryKeys)) {
            return false;
        }
        MyEntityPrimaryKeys that = (MyEntityPrimaryKeys) obj;
        EqualsBuilder eb = new EqualsBuilder();
        eb.append(RollNo, that.RollNo);
        eb.append(age, that.age);
        eb.append(tonMonth, that.tonMonth);
        eb.append(tonYear, that.tonYear);
        return eb.isEquals();
    }
}