Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate throws NullPointerException from HqlSqlWalker

Tags:

hibernate

hql

I have a web application, which has a search form and HQL is generated on the fly. Also, user can click on the column headers to sort items as needed. Some columns get ther data from very deep down the structure.

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 one fails miserably:

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 am I going wrong. Isn't such deep syntax supported or something?

Hibernate version is 3.2.1.

Sorry, forgot the stacktrace:

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)
.....
like image 590
Raidok Avatar asked Feb 21 '23 22:02

Raidok


1 Answers

As @JBNizet correctly pointed out, the problem was that one of the classes (named address to be exact) on the path was not an entity, it was an embeddable object and thus it doesn't need to be joined.

So the second query written correctly in my case would be:

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
like image 144
Raidok Avatar answered Mar 02 '23 15:03

Raidok