Hi, I'm using JPA 2.0, Hibernate 3.5.0-Beta4, MySQL 5.1 on Glassfish v3. I want to limit the polymorphic query results using JPA 2.0 syntax (TYPE) :
SELECT e FROM Employee e WHERE TYPE(e) IN (FullTime, Executive)
but it seems that Hibernate 3.5.0-Beta4 doesn't support this syntax because in the generated sql query the 'type' keyword remains unmapped and MySQL throws an Exception:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(type(employee0_.id) in (5))' at line 1
So I tried with Criteria API:
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Employee> criteria = criteriaBuilder.createQuery(Employee.class); Root<Employee> employee= criteria.from(Employee.class); criteria.where(employee.type().in(FullTime.class,Executive.class));
But I received an Exception:
Caused by: java.lang.IllegalArgumentException: Unexpected call on EntityTypeExpression#render at org.hibernate.ejb.criteria.expression.PathTypeExpression.render(PathTypeExpression.java:48) at org.hibernate.ejb.criteria.predicate.InPredicate.render(InPredicate.java:164) at org.hibernate.ejb.criteria.QueryStructure.render(QueryStructure.java:258) at org.hibernate.ejb.criteria.CriteriaQueryImpl.render(CriteriaQueryImpl.java:340) at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:150) at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:416) at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:489)
So I opened the PathTypeExpression class and in the render method I saw :
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) { // todo : is it valid for this to get rendered into the query itself? throw new IllegalArgumentException( "Unexpected call on EntityTypeExpression#render" ); }
Then I switched to use Hibernate custom query syntax (class) :
SELECT e FROM Employee e WHERE (e.class) IN (FullTime, Executive)
Ok this works fine. But when I add a join with another entity (Department) that has the same TABLE_PER_CLASS inheritance strategy of entity Employee, I have a new problem:
SELECT e FROM Employee e JOIN e.departments d WHERE d.code = 'D1' and (e.class) IN (FullTime, Executive)
GRAVE: Column 'clazz_' in where clause is ambiguous Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute query
How can I restrict the polymorphic query results in Hibernate using JPA 2.0 standard syntax?
Thanks in advance Domenico
|