休眠异常“ PropertyValueException:非null属性引用一个> null或瞬时值”。我被困住了,不知道该怎么办

时间:2019-02-19 11:56:33

标签: java hibernate jpa orm hql

Here you can see the entity-relationship diagram of my database with associations

我正在NetBeans中使用Apache Derby开发一个Hibernate库存管理程序。该应用程序将要求用户从合作伙伴数据库中预先输入的项目/合作伙伴中进行选择,然后输入日期(请参见上方的ERD)。用户可以选择从库存进货还是出库。假设这将在Advicenote表中创建一个Advicenote条目,如下所示:

Selection user interface

然后,在下一个窗口中,用户将能够实际设置与通知相关的Transaction的值:

Next input window

这是带有实现此目的的侦听器的代码:

private void deliveryButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
    advice.setAdvicedate((Date) this.dateSpinner.getValue());
    advice.setInOrOut((short)1);
    Criteria criteria = session.createCriteria(Partner.class);
    partner = (Partner) criteria.add(Restrictions.eq("partnerName", this.partnerComboBox.getSelectedItem())).uniqueResult();
    System.out.println("partner");
    advice.setPartner(partner);
    session.save(advice);
    transaction.commit();
    session.close();
    delivery.setVisible(true);
}                                              

private void transferButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
    advice.setAdvicedate((Date) this.dateSpinner.getValue());
    advice.setInOrOut((short)-1);
    Criteria criteria = session.createCriteria(Partner.class);
    partner = (Partner) criteria.add(Restrictions.eq("partnerName", this.partnerComboBox.getSelectedItem().toString())).uniqueResult();
    advice.setPartner(partner);
    session.save(advice);
    transaction.commit();
    session.close();
    transfer.setVisible(true);
} 

以下是这些类的XML映射文件:

合作伙伴课程

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="classes.Partner" table="PARTNER" schema="APP">
        <id name="partnerId" type="int">
            <column name="PARTNER_ID"/>
            <generator class="native"/>
        </id>
        <property name="partnerName" type="string" column="PARTNER_NAME" length="100"/>
        <set name="advicenotes" table="ADVICENOTE" inverse="false" cascade="all" lazy="true" fetch="select">
            <key column="PARTNER_ID" not-null="true"/>
            <one-to-many class="classes.Advicenote" />
        </set>
            </class>
</hibernate-mapping>

通知单班

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 9, 2019, 3:57:52 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="classes.Advicenote" table="ADVICENOTE" schema="APP" optimistic-lock="version">
        <id name="advicenoteId" type="int" column="ADVICENOTE_ID">
            <generator class="native" />
        </id>
        <property name="advicedate" type="date" column="ADVICE_DATE" length="10" />
        <property name="inOrOut" type="java.lang.Short" column="IN_OR_OUT" />
        <many-to-one 
            name="partner" class="classes.Partner"/>  
        <set name="transactions" table="TRANSACTIONS" inverse="false" cascade="all" lazy="true" fetch="select">       
            <key column="ADVICENOTE_ID" not-null="true"/>
            <one-to-many class="classes.Transactions"/>
        </set>
    </class>
</hibernate-mapping>

但是,我收到此错误:

  

线程“ AWT-EventQueue-0”中的异常   org.hibernate.PropertyValueException:非null属性引用一个   空值或瞬时值:   classes.Advicenote._classes.Partner.advicenotesBackref位于   org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:111)     在   org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:55)     在   org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:115)     在   org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:69)     在org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:645)     在   org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:282)     在   org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263)     在   org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:317)     在   org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:332)     在   org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)     在   org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:196)     在   org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:127)     在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)     在   org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)     在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)     在   org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)     在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)     在org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:713)     在org.hibernate.internal.SessionImpl.save(SessionImpl.java:705)处   org.hibernate.internal.SessionImpl.save(SessionImpl.java:700)在   delivery.view.Selection.deliveryButtonActionPerformed(Selection.java:190)     在delivery.view.Selection $ 2.actionPerformed(Selection.java:106)在   java.desktop / javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)     在   java.desktop / javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2308)     在   java.desktop / javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)     在   java.desktop / javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)     在   java.desktop / javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)     在   java.desktop / java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)     在   java.desktop / java.awt.Component.processMouseEvent(Component.java:6632)     在   java.desktop / javax.swing.JComponent.processMouseEvent(JComponent.java:3342)     在java.desktop / java.awt.Component.processEvent(Component.java:6397)     在java.desktop / java.awt.Container.processEvent(Container.java:2263)     在   java.desktop / java.awt.Component.dispatchEventImpl(Component.java:5008)     在   java.desktop / java.awt.Container.dispatchEventImpl(Container.java:2321)     在java.desktop / java.awt.Component.dispatchEvent(Component.java:4840)     在   java.desktop / java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)     在   java.desktop / java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)     在   java.desktop / java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)     在   java.desktop / java.awt.Container.dispatchEventImpl(Container.java:2307)     在java.desktop / java.awt.Window.dispatchEventImpl(Window.java:2772)     在java.desktop / java.awt.Component.dispatchEvent(Component.java:4840)     在   java.desktop / java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)     在java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:721)在   java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:715)在   java.base / java.security.AccessController.doPrivileged(本机方法)     在   java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)     在   java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)     在java.desktop / java.awt.EventQueue $ 5.run(EventQueue.java:745)在   java.desktop / java.awt.EventQueue $ 5.run(EventQueue.java:743)在   java.base / java.security.AccessController.doPrivileged(本机方法)     在   java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)     在   java.desktop / java.awt.EventQueue.dispatchEvent(EventQueue.java:742)     在   java.desktop / java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)     在   java.desktop / java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)     在   java.desktop / java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)     在   java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)     在   java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)     在   java.desktop / java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我已经尝试了很多年了,但是没有运气。我会尽力帮助您!

1 个答案:

答案 0 :(得分:0)

您已定义了双向映射,但只设置了一侧。

您必须在partner中设置AdviceNote,并在advicenotes上填写Partner集:

advice.setPartner(partner);
partner.getAdviceNotes().add(advice);
session.save(advice);
transaction.commit();