Issue Details (XML | Word | Printable)

Key: HHH-993
Type: Improvement Improvement
Status: Open Open
Priority: Minor Minor
Assignee: Unassigned
Reporter: Johannes Rudolph
Votes: 12
Watchers: 6
Operations

If you were logged in you would be able to see more operations.
Hibernate Core

Criteria subquery without projection fails throwing NullPointerException

Created: 28/Sep/05 02:15 AM   Updated: 12/Sep/08 09:14 AM
Component/s: core
Affects Version/s: None
Fix Version/s: None

Time Tracking:
Not Specified

Issue Links:
Relates
 


 Description  « Hide
If you use a subquery in a criteria query and there is no projection, the execution of the query will fail by throwing a NullPointerException:

Caused by: java.lang.NullPointerException
at
org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(Crit
eriaQueryTranslator.java:298)
at
org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.ja
va:56)
at
org.hibernate.criterion.LogicalExpression.toSqlString(LogicalExpression.java
:39)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(Crit
eriaQueryTranslator.java:314)
...

I think it is expected behaviour that restrictions only work on 1-dimensional return values of subqueries so it would be nice if Hibernate would mention that somewhere.
It would be nice if you could just write:

Criteria crit = dbSession.createCriteria(Bla.class);
         DetachedCriteria detachedCrit = DetachedCriteria.forClass(Blub.class).add( Restrictions.eq("condition", Boolean.FALSE));
         crit.add( Restrictions.or( Restrictions.isNull("superBlub"),
         Subqueries.in("superBlub", detachedCrit ) ) );

since Hibernate entities could be seen as "entities" so Hibernate should automatically transform it into:

         DetachedCriteria detachedCrit = DetachedCriteria.forClass(Blub.class).add( Restrictions.eq("condition", Boolean.FALSE))
.setProjection( Projections.id() );
         crit.add( Restrictions.or( Restrictions.isNull("superBlub"),
         Subqueries.propertyIn("ueberPartner", detachedCrit ) ) );

 All   Comments   Work Log   Change History   FishEye      Sort Order: Ascending order - Click to sort in descending order
Johannes Rudolph added a comment - 28/Sep/05 02:23 AM
The last line should be
Subqueries.propertyIn("superBlub", detachedCrit ) ) );

Assaf Berg added a comment - 23/Jan/06 10:11 AM
Same here. This is extremely annoying, especially since i'm using Subqueries.exists(), which means the projection isn't really relevant.
In the very least this should throw a more informative exception. Probably it would be better if there would be a default projection set (like Projections.id()).