【问题标题】:JPA/ Hibernate function in multiselect NPE多选 NPE 中的 JPA/休眠功能
【发布时间】:2014-10-04 05:30:27
【问题描述】:

在多选(或 select(construct(...)) 中使用函数会导致 NullPointerException(Hibernate 4+),代码如下:

criteriaQuery.select(criteriaBuilder.construct(OrderCriteria.class,
       order.get(Order_.id),
        criteriaBuilder.function("array_to_string", String.class,
                criteriaBuilder.function("array_agg", String.class, employee.<String>get(Employee_.firstName)), criteriaBuilder.literal(",")),
    ));

抛出以下异常:

java.lang.NullPointerException
    org.hibernate.internal.util.ReflectHelper.getConstructor(ReflectHelper.java:354)
    org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:185)

我已将异常追溯到 Hibernate ConstructionNode::resolveConstructorArgumentTypes。

似乎 getDataType() 在 MethodeNode 上不存在(criteriaBuilder.function 创建一个 MethodNode)导致 NPE:

private Type[] resolveConstructorArgumentTypes() throws SemanticException {
    SelectExpression[] argumentExpressions = collectSelectExpressions();
    if ( argumentExpressions == null ) {
        // return an empty Type array
        return new Type[] {};
    }

    Type[] types = new Type[argumentExpressions.length];
    for ( int x = 0; x < argumentExpressions.length; x++ ) {
        types[x] = argumentExpressions[x].getDataType(); --> [types[x] == null with MethodNode] 
    }
    return types;
}

select 的构造方式有问题吗?

【问题讨论】:

    标签: java hibernate jpa jpa-2.0


    【解决方案1】:

    几个小时后发现不能以这种方式使用未注册的函数,导致这个非常不清楚的异常。

    解决方案:

    public class ExtendedPostgreSQL9Dialect extends PostgreSQL9Dialect{
    
        public ExtendedPostgreSQL9Dialect() {
            super();
    
            registerFunction("array_agg", new StandardSQLFunction("array_agg", StandardBasicTypes.STRING));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      相关资源
      最近更新 更多