I'm using Hibernate 3.0.5 with Firebird 1.5 as database.
Simplified scenario: I have a main table, let's call it Person, with a one-to-many subtable called Contacts.
Creating criteria for Person:
Code:
DetachedCriteria dc = DetachedCriteria.forClass(Person.class);
dc.add(Restrictions.eq("city", "Somewhere"));
Later I want to use the detached criteria to select records from the Contacts table:
Code:
Criteria criteria = session.createCriteria(Contacts.class);
criteria.add(property.forName("person").in(dc));
criteria.list();
I get this exception:
Code:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:298)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:56)
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)
at test.HibernateTest.main(HibernateTest.java:107)
After some debugging, I figured out that Hibernate expects some kind of projection. If there is none, you get a null pointer exception. Ok... so I added a projection to my detached criteria, like this:
Code:
DetachedCriteria dc = DetachedCriteria.forClass(Person.class);
dc.add(Restrictions.eq("city", "Somewhere"));
dc.setProjection(Projections.property("id"));
This actually worked, at least it generated an SQL. But Firebird didn't like that SQL, because there was an "as" in the subselect. Here's the SQL (some fields in the outer select left out)
Code:
select this_.ID as ID0_, ...(more)... from CONTACTS this_ where this_.PERSON in (select this0__.ID as y0_ from PERSON this0__ where this0__.CITY=?)
Exception:
Code:
Caused by: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, char 365
as
Apparently, Firebird doesn't allow the keyword "as" in subselects.
I tried to do the same with HQL instead of Criteria, using a query like this:
Code:
from Contacts c where c.person in (select p.id from Person p where p.city = 'Someplace')
Funny enough, that worked perfectly. So it just seems to be a problem with criteria. Unfortunately I can't use HQL, I need to make subselects with the criteria API. I'd be thankful for any hints how to achieve that.