首页 > 代码库 > hibernate HQL —— ReflectHelper.java:343

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.NullPointerExceptionat 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 propertiesFor 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 ) {toif ( 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,问题解决。