-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Workaround for NPE using DetachedCriteria with subqueries
PostPosted: Fri Nov 04, 2005 3:10 pm 
Newbie

Joined: Thu Feb 24, 2005 6:19 am
Posts: 9
Hi,

I noticed something strange using DetachedCriteria with subqueries: Listing the outer DetachedCriteria produces a NPE unless the inner DetachedCriteria is listed first.

This kind of defeats to purpose of DetachedCriteria for me, because I need access to the session while assembling the DetachedCriteria.

Is anyone aware of another workaround? An even better solution would be a join inside the employees subquery, but that's currently not possible AFAIK.

Any ideas?

Thanks, Dan.


Hibernate version: 3.0.5
Code between sessionFactory.openSession() and session.close():
Session session = // ...

DetachedCriteria addresses = DetachedCriteria.forClass(Address.class);
addresses.add(Restrictions.eq("city", "chicago"));
addresses.setProjection(Projections.property("id"));

DetachedCriteria employees = DetachedCriteria.forClass(Employee.class);
employees.add(Property.forName("address").in(addresses));
employees.setProjection(Projections.property("department"));

DetachedCriteria departments = DetachedCriteria.forClass(Department.class);
departments.add(Property.forName("id").in(employees));

Criteria crit = departments.getExecutableCriteria(session);

// unless this method is called before crit.list(),
// Hibernate fails with a NPE, see below

employees.getExecutableCriteria(session).list();

List result = crit.list();

Full stack trace of any exception that occurs:
java.lang.NullPointerException
at org.hibernate.criterion.SubqueryExpression.getTypedValues(SubqueryExpression.java:80)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getQueryParameters(CriteriaQueryTranslator.java:231)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:55)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:314)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
...

Database schema:
TABLE DEPARTMENT
-ID
-NAME

TABLE EMPLOYEE
-ID
-NAME
-DEPT_ID
-ADDRESS_ID

TABLE ADDRESS
-ID
-STREET
-CITY


Top
 Profile  
 
 Post subject: Working in 3.1?
PostPosted: Mon Jan 16, 2006 1:01 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
Are joins in Criteria subqueries fixed in the latest 3.1. Gavin suggested that he was going to work on this in a previous posting and couldn't located in JIRA.

Cheers,
Roll


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.