hibernate HQL —— ReflectHelper.java:343

最近碰到个郁闷的问题嵌套查询时

UserAddress userAddress = (UserAddress) this.findOne("select new UserAddress(uuid,regionByDistrict,regionByCountry,regionByProvince,regionByCity,addressName,consignee,email,address,zipcode,tel,mobile,signBuilding,bestTime) from UserAddress ua where ua.uuid in (select u.userAddress.uuid from Users u where u.uuid = ?)", new Object[]{users.getUuid()});

执行这条语句的时候报错

java.lang.NullPointerException
    at org.hibernate.util.ReflectHelper.getConstructor(ReflectHelper.java:343)
    at org.hibernate.hql.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:137)
    at org.hibernate.hql.ast.tree.ConstructorNode.prepare(ConstructorNode.java:111)
    at org.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:966)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2068)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1932)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1476)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:580)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1282)
    at $Proxy109.createQuery(Unknown Source)

查看了hibernate官网的解释

During a session.createQuery like:

Query query6 = session.createQuery("select new Something(id, event, dummyField) " +
"from Something" );
A NPE is thrown:

Exception in thread "main" java.lang.NullPointerException
at org.hibernate.util.ReflectHelper.getConstructor(ReflectHelper.java:343)
at org.hibernate.hql.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:176)
at org.hibernate.hql.ast.tree.ConstructorNode.prepare(ConstructorNode.java:150)
at org.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:996)

I join an test case.

All versions of hibernate seems affected (from 3.2.6.ga to 3.6.0.RC2).

This is a minor issue since an easy workaround is available: you just have to use an alias to qualifie the properties
For the preceding example, we will have:

Query query3 = session.createQuery("select new Something(sthg.id, sthg.event, sthg.dummyField) " +
"from Something sthg" );

Possible fix:
As described in the testCase, I fix this by a one character fix:
In org.hibernate.hql.ast.HqlSqlWalker in method isNonQualifiedPropertyRef,
change from
if ( fromElements.size() == 1 ) {
to
if ( fromElements.size() >= 1 ) {

This could means that when a property is not qualified, we leave a chance for this property to be found on the first explicit element of the from clause.

I test this fix on the 3.5.5-Final code and I was able to run the test suite without failures.

I can‘t think of a test case in which this fix could lead to a regression but I‘m not an Hibernate Guru so comments are welcome.

修改代码执行

UserAddress userAddress = (UserAddress) this.findOne("select new UserAddress(ua.uuid,ua.regionByDistrict,ua.regionByCountry,ua.regionByProvince,ua.regionByCity,ua.addressName,ua.consignee,ua.email,ua.address,ua.zipcode,ua.tel,ua.mobile,ua.signBuilding,ua.bestTime) from UserAddress ua where ua.uuid in (select u.userAddress.uuid from Users u where u.uuid = ?)", new Object[]{users.getUuid()});
            

OK,问题解决。

hibernate HQL —— ReflectHelper.java:343

时间: 2024-10-10 02:05:58

hibernate HQL —— ReflectHelper.java:343的相关文章

Java学习笔记-Hibernate HQL查询

Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库的存取都与Session有关Session由SessionFactory创建,是线程安全的Thread-Safe,可以让多个线程同时存取SessionFactory而不会有数据共享的问题 Hibernate中Session的解释 :http://blog.csdn.net/shrek_xu/arti

Exception in thread &quot;main&quot; org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User user where user.name=?0 and user.pass=?1] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180

Hibernate HQL查询:

Hibernate HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此Hibernate将HQL查询方式立为官方推荐的标准查询方式,HQL查询在涵盖Criteria查询的所有功能的前提下,提供了类似标准SQL语句的查询方式,同时也提供了更加面向对象的封装.完整的HQL语句形势如下:Select/update/delete…… from …… where …

hibernate hql 大全

hibernate hql 大全 http://blog.chinaunix.net/article.php?articleId=47942&blogId=1655 HQL: Hibernate查询语言 Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可以理解如继承.多态 和关联之类的概念. 第 15 章 HQL: Hibernate查询语言 Hibernate配备了一种非常强大的查询

Spring Data之@Query中的org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML

1. 环境准备 Spring , Spring Data, JPA, HIbernate, JDK 1.7 2.  问题提出 在使用Spring Data 来更新数据之时,爆出了如下错误信息: @Query("UPDATE User u SET u.state = ?1 WHERE u.server.id = ?2") public void updateAllUsers(long state, long serverid); 错误信息如下: org.springframework.d

Hibernate HQL查询语句总结

Hibernate HQL查询语句总结 1. 实体查询:有关实体查询技术,其实我们在先前已经有多次涉及,比如下面的例子:String hql="from User user ";List list=session.CreateQuery(hql).list();上面的代码执行结果是,查询出User实体对象所对应的所有数据,而且将数据封装成User实体对象,并且放入List中返回.这里需要注意的是,Hibernate的实体查询存在着对继承关系的判定,比如我们前面讨论映射实体继承关系中的E

org.hibernate.hql.QueryExecutionRequestException:org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations【异常】

springData学习资料 [http://blog.csdn.net/lw_power/article/details/51296353] [JPA报错]org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hiberna

weblogic 下异常 org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken

项目之前在 Tomcat 环境下一直都正常运行,今天应客户要求需要迁移到 webLogic 10.3.6 下, 部署后竟然抛出了 org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken 异常,经过一番搜索后弄明白了问题的产生原因及解决方法. 问题原因 Hibernate3 采用新的基于 antlr 的 HQL/SQL 查询翻译器,在 hibernate3 中需要用到 antlr,然而

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: table is not Mapped问题解决

引言: 在基于SpringData/JPA来快速开发若干功能过程中,碰到了table is not Mapped问题,经过一番辛苦的调试测试之后,才发现了一个@Entity的属性name的妙用. 1. 问题的提出 场景描述: 在开发中,做几个功能类似的模块,但代码需要独立,方便后续的独立部署.故出现了很多包路径不同,但是类的名称类似的类.在Model中定义了很多名称相同的实体类,都是以@Entity来定义的. @Entity @Table(name="table1") public c