• 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,问题解决。

  • 相关阅读:
    推荐一本SQL经典书籍
    准备升级包包版游戏大厅
    《博客园精华集软件工程分册》第三轮筛选结果
    (翻译)《Expert .NET 2.0 IL Assembler》 第八章 基本类型和签名(一)
    如何输入人名间的顿号
    推荐一个下名人传记电子书的好地方
    asp.net 2.0 中使用web.config存储数据库连接字符串
    Asp.Net小技巧之在client端调用server端事件:
    C#编码好习惯
    把ip转换成对应的城市名
  • 原文地址:https://www.cnblogs.com/BrightMoon/p/3836285.html
Copyright © 2020-2023  润新知