在这里您可以看到我的数据库与关联的实体关系图
我正在使用NetBeans中的Apache Derby开发一个Hibernate库存管理程序。该应用程序将要求用户从合作伙伴数据库中预先输入的项目/合作伙伴中进行选择并输入日期(参见上面的ERD)。用户可以选择物品是从库存中传入还是传出。假设这将在Advicenote表中创建一个Advicenote条目,如下所示:
选择用户交互界面
然后在下一个窗口中,用户将能够实际设置与Advicenote关联的Transaction值:
下一个输入窗口
这是将实现此功能的侦听器的代码:
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>
Advicenote类
<?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>
但是,我得到这个错误:
我一直在努力解决这个问题,但没有成功。我会感谢每一个帮助!
您已经定义了双向映射,但您只设置了一面。
您必须在AdviceNote
中设置合作伙伴
,并在合作伙伴
上填写建议
设置:
advice.setPartner(partner);
partner.getAdviceNotes().add(advice);
session.save(advice);
transaction.commit();