Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3.2.0cr2
Mapping documents:
using annotation mapping
Code between sessionFactory.openSession() and session.close():
Code:
DetachedCriteria dc = DetachedCriteria.forClass(Debt.class);
dc.add(Restrictions.eq("client", getClient()));
Criterion crit = Property.forName("debt").in(dc);
DetachedCriteria countCrit = DetachedCriteria.forClass(Case.class);
countCrit.add(crit);
countCrit.setProjection(Projections.rowCount());
Integer rowCount = (Integer) getHibernateTemplate()
.findByCriteria(countCrit).get(0);
Full stack trace of any exception that occurs:
java.lang.NullPointerException
at
# org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:318)
# org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:56)
# org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:334)
# org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:71)
# org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:68)
# org.hibernate.impl.SessionImpl.list(SessionImpl.java:1543)
# org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
# org.springframework.orm.hibernate3.HibernateTemplate$37.doInHibernate(HibernateTemplate.java:988)
# org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
# org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:978)
# org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:971)
# my.dao.DaoImpl.count(DaoImpl.java:33)
So, the problem is that if i set projection to criteria that has a subquery then i get NPE from hibernate.
In SQL terms, i would like to see (select count(*) from case where case.debt_id in (select debt_id from debts where client_id = 1)), which should be rather simple query.
As far as i can see, CriteriaJoinWalker starts to assemble a query, asks outer criteria for its projection (which is count(*)) and then proceeds to ask subquery criteria for projection too, but subquery criteria projection is null (and as far as i can see, that's correct).
What i'm not sure about is the solution. I can't add projection to subquery, and besides, it wouldn't be correct to do so as far as i can see.
Am i assembling my criteria wrong? Mappings are correct and work, with or without projections.
Thank you for your time.
Tanel Unt