【问题标题】:java.lang.IllegalStateException: No supertype found on queryjava.lang.IllegalStateException:在查询中找不到超类型
【发布时间】:2013-03-18 16:51:54
【问题描述】:

我有以下

@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

【问题讨论】:

    标签: java hibernate jpa jpa-2.0


    【解决方案1】:

    我已经通过在实体类中使用@EmbeddedId 和在主键类中使用@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
    

    【讨论】:

      【解决方案2】:

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

      • Class MyEntity - 这是我的表的实际实体类。 “OtherFields”是那些不属于主键的字段。
      • Class MyEntityPrimaryKeys - 这是为我的复合键创建的类,它为我的“MyEntity”类创建主键。这里 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();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-04
        • 2015-03-23
        • 1970-01-01
        • 2016-10-22
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 1970-01-01
        相关资源
        最近更新 更多