使用HQL的新对象 - StandardAnsiSqlAggregationFunctions上的NPE,确定JdbcTypeCode

时间:2013-08-21 15:57:07

标签: java sql hibernate hql

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

以下是一些代码:

映射的bean:

@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 */
}

Stacktrace:

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

1 个答案:

答案 0 :(得分:10)

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

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

有时,HQL解析器在解析步骤中将标识符解析为有效,但在执行期间失败并出现奇怪的异常。看here