I have a JSF webapp throwing an exception (see below) when I’m trying to query for entities mapped with Hibernate. What am I doing wrong? Or is it a Bug in Hibernate? How can I fix that? Thanks for your help 🙂
Here are the relevant classes:
Base class ShipmentUnit with some sub-classes:
@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.
}
The ShipmentAction class:
@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
}
The ShipmentActionID class also has the unit and listIndex properties with the same signatures on the getter and setter methods.
Now, I want to display a h:dataTable with a LazyDataModel<ShipmentAction>. The implementation of the data model uses the Criteria API to (1) count and (2) query for those shipment action entities, like so:
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();
At the moment the TypedQuery should be created with em.createQuery(...) an exception is being thrown:
[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)
I am using Hibernate Version 4.0.0.Final, running on JBoss 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>
I found a solution to my problem. I changed the mapping of the dependent class
ShipmentActionby using an@EmbeddedIdinstead of two@Idannotations. In case you’re interested, here is the updated class (the mapping of theShipmentUnitclass has not changed 1)Okay, this seems to work and my
LazyDataModel<ShipmentAction>likes it now 🙂But the only strange thing is, that this only works with JPA annotations on the fields, Hibernate can’t cope with these annotations on the getter-methods anymore (at least on the classes affected by my change). Any thoughts on that one?
1 One other little change: the
@javax.persistence.OrderByannotation atShipmentUnit.getActions()had to be replaced with@org.hibernate.annotations.OrderByfor whatever reason.