Hibernate throws NullPointerException from HqlSqlWalker
I have a web application that has a search form and the HQL is generated on the fly. In addition, the user can click the column headings to sort the items as needed. Some columns receive data from very deep structures.
I have this HQL, for example, which works flawlessly:
SELECT s FROM Application s
LEFT JOIN s.product AS product
LEFT JOIN product.originCountry AS origin
WHERE s.nr = ? ORDER BY origin.name ASC
But this fails:
SELECT s FROM Application s
LEFT JOIN s.product AS product
LEFT JOIN product.producer AS producer
LEFT JOIN producer.address AS address
LEFT JOIN address.country AS country
WHERE s.nr = ? ORDER BY country.name ASC
Can someone point out where I am going wrong. Isn't such deep syntax supported or something else?
The hibernation version is 3.2.1.
Sorry forgot the stack:
2012-04-04 18:59:42,198 ERROR [foo.impl.ServiceImpl] java.lang.NullPointerException
java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:312)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3275)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3067)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at sun.reflect.GeneratedMethodAccessor336.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1192)
at $Proxy90.createQuery(Unknown Source)
.....
source to share
As @JBNizet correctly pointed out, the problem was that one of the classes (with a name address
to be precise) in the path was not an entity, it was an embeddable object and therefore did not need to be attached.
So the second query, written correctly in my case, would look like this:
SELECT s FROM Application s
LEFT JOIN s.product AS product
LEFT JOIN product.producer AS producer
LEFT JOIN producer.address.country AS country
WHERE s.nr = ? ORDER BY country.name ASC
source to share