Hibernate throws NullPointerException from HqlSqlWalker

拜拜、爱过 提交于 2019-12-06 06:51:29

问题


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)
.....

回答1:


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


来源:https://stackoverflow.com/questions/10015261/hibernate-throws-nullpointerexception-from-hqlsqlwalker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!