JPA 2.0 / Hibernate:找不到超类型

时间:2012-02-07 16:08:55

标签: hibernate jpa orm jpa-2.0 criteria-api

当我尝试查询使用Hibernate映射的实体时,我有一个JSF webapp抛出异常(见下文)。我究竟做错了什么?或者它是Hibernate中的Bug?我该如何解决这个问题?谢谢你的帮助:)

以下是相关课程:

基类ShipmentUnit包含一些子类:

@Entity
@Table(name = "tat_shipment_unit")
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "unit_type", discriminatorType = CHAR, length = 1)
@DiscriminatorValue("_")
public abstract class ShipmentUnit implements Serializable {
    private Long id;
    private List<ShipmentAction> actions;

    @Id @GeneratedValue
    public Long getId() { return id; }  // and corresponding setter

    @OneToMany(mappedBy = "unit")
    @OrderBy("listIndex")
    public List<ShipmentAction> getActions() { return actions; }  // and setter

    // hashCode, equals etc.
}

ShipmentAction类:

@Entity
@Table(name = "tat_shipment_action")
@IdClass(ShipmentActionID.class)
public class ShipmentAction implements Serializable {
    private ShipmentUnit unit;
    private int listIndex;

    @Id @ManyToOne @NotNull
    public ShipmentUnit getUnit() { return unit; }  // and setter

    @Id @Column(name = "list_index", nullable = false)
    public int getListIndex() { return listIndex; } // and setter
}

ShipmentActionID类还具有unitlistIndex属性,在getter和setter方法上具有相同的签名。

现在,我想要显示h:dataTable LazyDataModel<ShipmentAction>。数据模型的实现使用Criteria API来(1)计数和(2)查询那些出货操作实体,如下所示:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Number> query = cb.createQuery(Number.class);
Root<ShipmentAction> root = query.from(ShipmentAction.class);
Predicate predicates = ...
int total = em.createQuery(
    query.select(cb.count(root).where(predicates)
).getSingleResult().intValue();

目前应使用TypedQuery创建em.createQuery(...),但会抛出异常:

[SEVERE] Error Rendering View[/tat/report/actions.xhtml]: java.lang.IllegalStateException: No supertype found
at org.hibernate.ejb.metamodel.AbstractIdentifiableType.requireSupertype(AbstractIdentifiableType.java:85)
at org.hibernate.ejb.metamodel.AbstractIdentifiableType.getIdType(AbstractIdentifiableType.java:173)
at org.hibernate.ejb.criteria.expression.function.AggregationFunction$COUNT.renderArguments(AggregationFunction.java:110)
at org.hibernate.ejb.criteria.expression.function.ParameterizedFunctionExpression.render(ParameterizedFunctionExpression.java:94)
at org.hibernate.ejb.criteria.expression.function.BasicFunctionExpression.renderProjection(BasicFunctionExpression.java:71)
at org.hibernate.ejb.criteria.QueryStructure.render(QueryStructure.java:250)
at org.hibernate.ejb.criteria.CriteriaQueryImpl.render(CriteriaQueryImpl.java:338)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:223)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:619)

我正在使用在JBoss AS 7上运行的Hibernate版本4.0.0.Final。

        <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.spec.javax.ejb</groupId>
        <artifactId>jboss-ejb-api_3.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.spec.javax.annotation</groupId>
        <artifactId>jboss-annotations-api_1.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.0.0.Final</version>
        <scope>provided</scope>
    </dependency>

1 个答案:

答案 0 :(得分:4)

我找到了解决问题的方法。我使用ShipmentAction而不是两个@EmbeddedId注释更改了依赖类@Id的映射。如果你感兴趣,这里是更新的类(ShipmentUnit类的映射没有改变 1

@Entity
@Table(name = "tat_shipment_action")
public class ShipmentAction implements Serializable {
    private @EmbeddedId Key id;
    private @MapsId("unit_id") @ManyToOne ShipmentUnit unit;

    public ShipmentAction() {
        id = new Key();
    }

    public ShipmentUnit getUnit() { return unit; }  // and setter

    public int getListIndex() {
        return id.list_index;
    }

    public void setListIndex(int index) {
        id.list_index = index;
    }

    @Embeddable
    public static class Key implements Serializable {
        public long unit_id;
        public int list_index;
        // hashCode() and equals()
    }
}

好的,这似乎有效,我的LazyDataModel<ShipmentAction>现在喜欢它:-)
但唯一奇怪的是,这只适用于字段上的JPA注释,Hibernate再也无法处理getter-methods上的这些注释(至少在受我的更改影响的类上)。关于那个的任何想法?

1 另一个小变化:@javax.persistence.OrderBy ShipmentUnit.getActions()注释必须用@org.hibernate.annotations.OrderBy替换为任何原因。