一尘不染

具有HQL的新对象-StandardAnsiSqlAggregationFunctions上的NPE,确定JdbcTypeCode

hibernate

我知道这个问题已经提出了很多,但是我似乎遇到了另一个问题。我看到Hibernate
中有一个bug,它与SumFunction忽略了自定义用户类型有关,但是据我所知,我没有使用自定义用户类型。我基本上是在尝试创建一个新对象,该对象是现有映射bean的汇总,但是我收到了NullPointerException针对某些Hibernate方言函数的。我尝试将Double和BigDecimal都用作value字段类型,但在部署时都向我提供了相同的错误消息。请帮忙!

这是一些代码:

映射的豆:

@Entity
@Table(name = "v_summary_report")
public class SummaryReportView implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "id")
    private String id;

    @Column(name = "region")
    private String region;

    @Column(name = "market")
    private String market;

    @Column(name = "proj_year_month")
    private String projectionYearMonth;

    @OneToOne(cascade={CascadeType.DETACH})
    @JoinColumn(name="attrib_id", referencedColumnName="attrib_id")
    private Attribute attribute;

    @Column(name = "total_value")
    private BigDecimal value;

    /* other stuff */
}

命名查询被调用:

    @NamedQuery(name = "findSummaryReportTotalByRegion", query = " SELECT new com.rac.projections.bean.SummaryReportTotal(srv.projectionYearMonth, srv.attribute, sum(value)) from SummaryReportView srv where srv.region = :region group by srv.projectionYearMonth, srv.attribute"),

Bean总定义:

public class SummaryReportTotal {

    private String projectionYearMonth;
    private Attribute attribute;
    private BigDecimal value;

    public SummaryReportTotal() {
        super();
    }

    public SummaryReportTotal(String projectionYearMonth, Attribute attribute, BigDecimal value) {
        this.projectionYearMonth = projectionYearMonth;
        this.attribute = attribute;
        this.value = value;
    }

    /* other stuff */
}

堆栈跟踪 :

Caused by: java.lang.NullPointerException
at org.hibernate.dialect.function.StandardAnsiSqlAggregationFunctions$SumFunction.determineJdbcTypeCode(StandardAnsiSqlAggregationFunctions.java:145)
at org.hibernate.dialect.function.StandardAnsiSqlAggregationFunctions$SumFunction.getReturnType(StandardAnsiSqlAggregationFunctions.java:157)
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findFunctionReturnType(SessionFactoryHelper.java:406)
at org.hibernate.hql.internal.ast.tree.AggregateNode.getDataType(AggregateNode.java:83)
at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructorArgumentTypes(ConstructorNode.java:166)
at org.hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:141)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1019)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2150)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2016)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1451)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:1032)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:506)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 45 more

阅读 280

收藏
2020-06-20

共1个答案

一尘不染

如果value以表别名作为前缀,我不会感到惊讶,因为其他字段可以解决该问题

... sum(srv.value) ...

有时,HQL解析器在解析步骤中将标识符解析为有效,但是在执行期间会因奇怪的异常而失败。

2020-06-20