Copy Predicate from a CriteriaQuery to another will cause QuerySyntaxException
------------------------------------------------------------------------------
Key: HHH-4603
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-4603
Project: Hibernate Core
Issue Type: Bug
Components: entity-manager
Affects Versions: 3.5.0-Beta-2
Reporter: zhouyanming
I build a CriteriaQuery and I want count it before fetch data for pagination,so I need
create a new CriteriaQuery for counting.
public long countByCriteria(CriteriaQuery cq) {
CriteriaBuilder cb = getCriteriaBuilder();
CriteriaQuery count = cb.createQuery(Long.class);
Root<T> root = count.from(cq.getResultType());
EntityType<T> entityType = metamodel.entity(cq.getResultType());
Expression idExpression = root.get(entityType.getSingularAttribute("id"));
count.select(cb.count(idExpression));
count.where(cq.getRestriction()); //here will cause exception
TypedQuery<Long> tq = em.createQuery(count);
return tq.getSingleResult();
}
@Test
public void testCountByCriteria() {
CriteriaBuilder cb = getCriteriaBuilder();
CriteriaQuery<User> cq = cb.criteriaQuery(User.class);
Root<User> root = cq.from(User.class);
EntityType<User> entityType = getMetamodel().entity(User.class);
Expression expression = root.get(entityType.getSingularAttribute("username"));
Predicate condition = cb.equal(expression, "admin");
cq.where(condition);
assertTrue(baseManager.countByCriteria(cq) > 0);
}
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException:
Invalid path: 'generatedAlias1.username' [select count(generatedAlias0.id) from
model.User as generatedAlias0 where generatedAlias1.username=:param0]
at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:855)
at
org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:138)
at
org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:104)
at
org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.springframework.orm.jpa.JpaTemplate$CloseSuppressingInvocationHandler.invoke(JpaTemplate.java:404)
... 38 more
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Invalid path:
'generatedAlias1.username' [select count(generatedAlias0.id) from model.User as
generatedAlias0 where generatedAlias1.username=:param0]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:261)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1761)
at
org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:123)
... 48 more
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira