为什么Hibernate / JPA在查询中报告空指针异常?

时间:2018-08-18 17:45:39

标签: java hibernate jpa

我正在使用Hibernate的JPA和Java 8执行应该是一个简单的查询,并将结果存储在类实例的列表中。但是,当我执行代码时,Java在.createQuery行上给了我一个空指针异常。

我检查了传递给方法的变量,它们都包含有效的非空数据。当我在数据库上手动运行查询时检索到的行在任何接收到的列中均未显示空条目。查询中的所有列都是GradeSummary类和Board类的成员。

我很茫然。为什么此查询不起作用?

public List<GradeSummary> getGradeSummaryList(VdtsSysDB vdtsSysDB, BoardLoad boardLoad) {
    List<GradeSummary> gradeSummaries = vdtsSysDB
            .getEntityManager()
            .createQuery(
            "select new ca.vdts.grading.model.GradeSummary(grade, " +
            "count(*), " +
            "sum(footBoardMeasure)) " +
            "from Board " +
            "where boardLoad = :load " +
            "group by grade", GradeSummary.class)
            .setParameter("load", boardLoad)
            .getResultList();
    gradeSummaries.sort(Comparator.comparing(gradeSummary -> gradeSummary.getGrade().getName()));
    return gradeSummaries;
}

例外是:

18-08-18 13:28:50.701 ERROR java.lang.Throwable - Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
18-08-18 13:28:50.702 ERROR java.lang.Throwable -   at org.hibernate.dialect.function.StandardAnsiSqlAggregationFunctions$SumFunction.determineJdbcTypeCode(StandardAnsiSqlAggregationFunctions.java:200)
18-08-18 13:28:50.702 ERROR java.lang.Throwable -   at org.hibernate.dialect.function.StandardAnsiSqlAggregationFunctions$SumFunction.getReturnType(StandardAnsiSqlAggregationFunctions.java:158)
18-08-18 13:28:50.702 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findFunctionReturnType(SessionFactoryHelper.java:414)
18-08-18 13:28:50.703 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.ast.tree.AggregateNode.getDataType(AggregateNode.java:68)
18-08-18 13:28:50.703 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructorArgumentTypes(ConstructorNode.java:158)
18-08-18 13:28:50.703 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:133)
18-08-18 13:28:50.703 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1190)
18-08-18 13:28:50.704 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2366)
18-08-18 13:28:50.704 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2232)
18-08-18 13:28:50.704 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1503)
18-08-18 13:28:50.704 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:585)
18-08-18 13:28:50.704 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
18-08-18 13:28:50.705 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
18-08-18 13:28:50.705 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266)
18-08-18 13:28:50.705 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
18-08-18 13:28:50.705 ERROR java.lang.Throwable -   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
18-08-18 13:28:50.706 ERROR java.lang.Throwable -   at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
18-08-18 13:28:50.706 ERROR java.lang.Throwable -   at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
18-08-18 13:28:50.706 ERROR java.lang.Throwable -   at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
18-08-18 13:28:50.707 ERROR java.lang.Throwable -   at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553)
18-08-18 13:28:50.707 ERROR java.lang.Throwable -   at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
18-08-18 13:28:50.707 ERROR java.lang.Throwable -   at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:686)
18-08-18 13:28:50.707 ERROR java.lang.Throwable -   at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
18-08-18 13:28:50.707 ERROR java.lang.Throwable -   at ca.vdts.grading.model.Progress.getGradeSummaryList(Progress.java:18)
18-08-18 13:28:50.708 ERROR java.lang.Throwable -   at ca.vdts.grading.controllers.ProgressController.updateDisplay(ProgressController.java:155)
18-08-18 13:28:50.708 ERROR java.lang.Throwable -   at ca.vdts.grading.controllers.ProgressController.monitorSelected(ProgressController.java:125)
18-08-18 13:28:50.708 ERROR java.lang.Throwable -   at ca.vdts.grading.controllers.ProgressController.lambda$initialize$3(ProgressController.java:88)
18-08-18 13:28:50.708 ERROR java.lang.Throwable -   at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
18-08-18 13:28:50.708 ERROR java.lang.Throwable -   at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
18-08-18 13:28:50.709 ERROR java.lang.Throwable -   at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
18-08-18 13:28:50.709 ERROR java.lang.Throwable -   at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
18-08-18 13:28:50.709 ERROR java.lang.Throwable -   at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
18-08-18 13:28:50.709 ERROR java.lang.Throwable -   at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
18-08-18 13:28:50.709 ERROR java.lang.Throwable -   at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
18-08-18 13:28:50.710 ERROR java.lang.Throwable -   at javafx.scene.control.ComboBox$ComboBoxSelectionModel.lambda$new$154(ComboBox.java:494)
18-08-18 13:28:50.710 ERROR java.lang.Throwable -   at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
18-08-18 13:28:50.710 ERROR java.lang.Throwable -   at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
18-08-18 13:28:50.711 ERROR java.lang.Throwable -   at javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72)
18-08-18 13:28:50.711 ERROR java.lang.Throwable -   at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102)
18-08-18 13:28:50.711 ERROR java.lang.Throwable -   at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
18-08-18 13:28:50.711 ERROR java.lang.Throwable -   at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
18-08-18 13:28:50.711 ERROR java.lang.Throwable -   at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68)
18-08-18 13:28:50.712 ERROR java.lang.Throwable -   at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:215)
18-08-18 13:28:50.712 ERROR java.lang.Throwable -   at javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149)
18-08-18 13:28:50.712 ERROR java.lang.Throwable -   at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.lambda$createListView$323(ComboBoxListViewSkin.java:484)
18-08-18 13:28:50.712 ERROR java.lang.Throwable -   at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:349)
18-08-18 13:28:50.713 ERROR java.lang.Throwable -   at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
18-08-18 13:28:50.713 ERROR java.lang.Throwable -   at javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72)
18-08-18 13:28:50.713 ERROR java.lang.Throwable -   at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102)
18-08-18 13:28:50.713 ERROR java.lang.Throwable -   at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
18-08-18 13:28:50.714 ERROR java.lang.Throwable -   at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
18-08-18 13:28:50.714 ERROR java.lang.Throwable -   at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68)
18-08-18 13:28:50.714 ERROR java.lang.Throwable -   at javafx.scene.control.MultipleSelectionModelBase.select(MultipleSelectionModelBase.java:404)
18-08-18 13:28:50.714 ERROR java.lang.Throwable -   at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:356)
18-08-18 13:28:50.714 ERROR java.lang.Throwable -   at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1403)
18-08-18 13:28:50.715 ERROR java.lang.Throwable -   at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:256)
18-08-18 13:28:50.715 ERROR java.lang.Throwable -   at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:220)
18-08-18 13:28:50.716 ERROR java.lang.Throwable -   at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
18-08-18 13:28:50.716 ERROR java.lang.Throwable -   at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
18-08-18 13:28:50.716 ERROR java.lang.Throwable -   at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
18-08-18 13:28:50.717 ERROR java.lang.Throwable -   at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
18-08-18 13:28:50.717 ERROR java.lang.Throwable -   at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
18-08-18 13:28:50.717 ERROR java.lang.Throwable -   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
18-08-18 13:28:50.717 ERROR java.lang.Throwable -   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
18-08-18 13:28:50.718 ERROR java.lang.Throwable -   at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
18-08-18 13:28:50.718 ERROR java.lang.Throwable -   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
18-08-18 13:28:50.718 ERROR java.lang.Throwable -   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
18-08-18 13:28:50.719 ERROR java.lang.Throwable -   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
18-08-18 13:28:50.719 ERROR java.lang.Throwable -   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
18-08-18 13:28:50.719 ERROR java.lang.Throwable -   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
18-08-18 13:28:50.720 ERROR java.lang.Throwable -   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
18-08-18 13:28:50.720 ERROR java.lang.Throwable -   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
18-08-18 13:28:50.720 ERROR java.lang.Throwable -   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
18-08-18 13:28:50.721 ERROR java.lang.Throwable -   at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
18-08-18 13:28:50.721 ERROR java.lang.Throwable -   at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
18-08-18 13:28:50.721 ERROR java.lang.Throwable -   at javafx.event.Event.fireEvent(Event.java:198)
18-08-18 13:28:50.721 ERROR java.lang.Throwable -   at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
18-08-18 13:28:50.722 ERROR java.lang.Throwable -   at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
18-08-18 13:28:50.722 ERROR java.lang.Throwable -   at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
18-08-18 13:28:50.722 ERROR java.lang.Throwable -   at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
18-08-18 13:28:50.722 ERROR java.lang.Throwable -   at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
18-08-18 13:28:50.723 ERROR java.lang.Throwable -   at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
18-08-18 13:28:50.723 ERROR java.lang.Throwable -   at java.security.AccessController.doPrivileged(Native Method)
18-08-18 13:28:50.723 ERROR java.lang.Throwable -   at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
18-08-18 13:28:50.723 ERROR java.lang.Throwable -   at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
18-08-18 13:28:50.723 ERROR java.lang.Throwable -   at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
18-08-18 13:28:50.724 ERROR java.lang.Throwable -   at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
18-08-18 13:28:50.724 ERROR java.lang.Throwable -   at com.sun.glass.ui.View.notifyMouse(View.java:937)
18-08-18 13:28:50.724 ERROR java.lang.Throwable -   at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
18-08-18 13:28:50.724 ERROR java.lang.Throwable -   at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
18-08-18 13:28:50.725 ERROR java.lang.Throwable -   at java.lang.Thread.run(Thread.java:748)

gradeSummary类:

public class GradeSummary {
    private BoardGrade grade;
    private Integer pieceCount;
    private Integer footage;

    public GradeSummary(BoardGrade grade, Long pieceCount, Long footage) {
        this.grade = grade;
        this.pieceCount = pieceCount.intValue();
        this.footage = footage.intValue();
    }

    public BoardGrade getGrade() {
        return grade;
    }

    public Integer getPieceCount() {
        return pieceCount;
    }

    public Integer getFootage() {
        return footage;
    }
}

以及董事会的字段名称:

private LocalDateTime timeStamp;
private User grader;
private Species species;
private Integer length;
private Integer width;
private Integer surfaceMeasure;
private Integer thickness;
private Integer footBoardMeasure;
private BoardGrade grade;
private BoardModifier modifier;
private BigDecimal price;
private BigDecimal rate;
private Pack pack;
private Batch batch;
private PackDescriptor packDescriptor;
private BoardLoad boardLoad;

1 个答案:

答案 0 :(得分:2)

GradeSummary需要一个无参数的构造函数。通过提供自己的,您删除了默认的。

这样做的原因是JPA需要创建对象的实例,然后对其进行水化处理。它必须通过在类上调用newInstance()来开始,并且对参数一无所知。