一尘不染

Hibernate从HqlSqlWalker引发NullPointerException

hibernate

我有一个Web应用程序,该应用程序具有搜索表单,并且HQL是即时生成的。此外,用户可以单击列标题以根据需要对项目进行排序。一些列从非常深的结构中获取其他数据。

例如,我有这个HQL,它可以完美地工作:

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

但是这一失败惨败:

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

有人可以指出,我要去哪里错了。不支持这种深层语法吗?

hibernate版本是3.2.1。

抱歉,忘记了堆栈跟踪:

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

阅读 563

收藏
2020-06-20

共1个答案

一尘不染

正如@JBNizet正确指出的那样,问题是address路径上的一个类(确切地说是名称)不是实体,它是可嵌入的对象,因此不需要联接。

因此在我的情况下正确编写的第二个查询将是:

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
2020-06-20