Hibernate从HqlSqlWalker抛出NullPointerException

时间:2012-04-04 16:20:41

标签: hibernate hql

我有一个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)
.....

2 个答案:

答案 0 :(得分:7)

正如@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

答案 1 :(得分:0)

我试图在两个不同的数据库之间建立连接,但我遇到了同样的问题。 我使用了 Native Query 来解决这个问题。