2012-02-07 71 views
3

当我试图查询映射到Hibernate的实体时,我有一个JSF webapp抛出异常(见下文)。我究竟做错了什么?或者它是Hibernate中的一个Bug?我该如何解决这个问题?感谢您的帮助:)JPA 2.0/Hibernate:没有发现超类型

下面是相关的类:

基类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类对于getter和setter方法也具有相同签名的unitlistIndex属性。

现在,我想显示一个h:dataTableLazyDataModel<ShipmentAction>。数据模型的实现使用标准的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) 

我使用Hibernate的版本4.0.0.Final,AS 7

 <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> 

回答

4

运行在JBoss上我发现了一个解决我的问题。我通过使用@EmbeddedId而不是两个@Id注释来更改从属类ShipmentAction的映射。如果你有兴趣,这里是更新类(ShipmentUnit类的映射并没有改变)

@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上的这些批注(至少在受我更改影响的类上)。对此有任何想法?其他

一个小的变化:在ShipmentUnit.getActions()@javax.persistence.OrderBy注释必须与@org.hibernate.annotations.OrderBy被替换无论出于何种原因。

+0

我需要这个。但是,当我切换到嵌入式密钥时,我还需要使用'@Query(“UserRel r select r from r.key.user1Id =?1”)注释我的JpaRepository方法。 \t public Iterable findByUser1Id(long id1) ;' – Spencer 2013-03-20 20:57:37

相关问题