我有3个名为“Invoice”,“Selling”,“Invoice”的mysql数据库表.ER图如下。这是一个用于计费应用的简单数据库。
发票表保存有关发票的数据,销售表保留为特定发票购买的物品。项目表保存有关每个项目的详细信息。完成购买后,将发出发票。这就是我使用Hibernate.Hsession保存到数据库的方式,这是我的Sessionfactory所在的并返回一个会话。Date invoiceD = invoice.invoiceDate.getDate();
成功返回发票日期。
public boolean saveInvoice() {
try {
Session session = HSession.getSession();
Transaction transaction = session.beginTransaction();
Date invoiceD = invoice.invoiceDate.getDate();
Entity.Invoice inv = new Entity.Invoice();
final int invcNo = this.invoiceNo;
inv.setInvNo(invcNo);
inv.setDateInv(invoiceD);
inv.setValue(totalValue);
inv.setInvDisc(discountAmount);
inv.setInvNetvalue(netValue);
inv.setInvPaid(paid);
Set<Selling> sellings = new HashSet<Selling>();
Iterator iterator = purchasingList.iterator();
while (iterator.hasNext()) {
Item item = (Item) iterator.next();
Selling sel = new Selling();
SellingId sId = new SellingId();
sId.setInvoiceInvNo(invcNo);
sId.setItemItemid(item.getItemid());
System.out.println("Selling .." + item.getItemid());
sel.setId(sId);
sel.setSellQty(item.getQty());
sel.setSoldPrice(item.getSellingPrice());
sel.setInvoice(inv);
sel.setItem(item);
sellings.add(sel);
}
inv.setSellings(sellings);
session.save(inv);
transaction.commit();
session.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
我的问题仅保存了发票数据,但没有设置为发票inv.setSellings(sellings);
的销售详细信息(发票表有一对可能关系,所有采购商品对象都添加到{{然后将该集添加到发票对象中。)
但是当我保存发票和销售单独两者都成功保存。(在一个会话中保存发票详细信息,调用其事务,然后再将项目保存在另一个单独的会话中)。(Hibernate映射和创建实体)完成了Netbeans IDE)
请任何人告诉我的位置&amp;问题是什么,我应该在哪里检查。另请告诉我如何在netbeans IDE中查看hibernate sql执行。
父发票映射;
Set
子实体;卖
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 18, 2011 5:03:10 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="Entity.Invoice" table="invoice" catalog="ssm">
<id name="invNo" type="java.lang.Integer">
<column name="inv_no" />
<generator class="identity" />
</id>
<many-to-one name="customer" class="Entity.Customer" fetch="select">
<column name="Customer_cust_id" />
</many-to-one>
<property name="dateInv" type="date">
<column name="date_inv" length="0" not-null="true" />
</property>
<property name="value" type="double">
<column name="value" precision="22" scale="0" not-null="true" />
</property>
<property name="invDisc" type="java.lang.Double">
<column name="inv_disc" precision="22" scale="0" />
</property>
<property name="invNetvalue" type="double">
<column name="inv_netvalue" precision="22" scale="0" not-null="true" />
</property>
<property name="invPaid" type="double">
<column name="inv_paid" precision="22" scale="0" not-null="true" />
</property>
<set name="cheqIncomes" inverse="true">
<key>
<column name="Invoice_inv_no" not-null="true" />
</key>
<one-to-many class="Entity.CheqIncome" />
</set>
<set name="sellings" inverse="true">
<key>
<column name="Invoice_inv_no" not-null="true" />
</key>
<one-to-many class="Entity.Selling" />
</set>
<set name="receipts" inverse="true">
<key>
<column name="Invoice_inv_no" not-null="true" />
</key>
<one-to-many class="Entity.Receipt" />
</set>
</class>
</hibernate-mapping>
答案 0 :(得分:3)
在Invoice实体的Hibernate映射中,您需要将级联类型设置为“persist”或可能设置为“all”,具体取决于您的需要。请参阅http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html
上的Hibernate文档答案 1 :(得分:2)
检查RichW上面指出的实体映射。将cascade =“all”添加到Set定义。
<set name="sellings" inverse="true" cascade="all">
<key>
<column name="Invoice_inv_no" not-null="true" />
</key>
<one-to-many class="Entity.Selling" />
</set>