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

4

2 回答 2

0

我已经通过@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
于 2013-03-18T18:18:36.307 回答
0

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

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

我的实体.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();
    }
}
于 2018-04-24T11:36:37.680 回答