簡體   English   中英

java.lang.IllegalStateException:在查詢中找不到超類型

[英]java.lang.IllegalStateException: No supertype found on query

我有以下

@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在上一行中java.lang.IllegalStateException: No supertype found 如何解決或解決此問題? 看起來有一個錯誤 ,對此有什么解決方案嗎?

我正在使用Hibernate 4.1.0.Final

我已經通過在實體類中使用@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

對於使用@EmbeddedId的情況,這是我的解決方案。 我在Entity類的一個類中自己編寫了此代碼。

  • 類MyEntity-這是我的表的實際Entity類。 “ OtherFields”是不屬於主鍵的那些字段。
  • 類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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM