【发布时间】:2011-09-23 05:29:25
【问题描述】:
我有 3 个名为“Invoice”、“Selling”、“Invoice”的 mysql 数据库表。ER 图如下。这是一个用于计费应用程序的简单数据库。
Invoice 表保存有关发票的数据,Selling 表保存为特定发票购买的物品。项目表保存有关每个项目的详细信息。购买完成后,将开具发票。这就是我使用 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); 的销售详细信息(发票表具有一对可能关系,并且所有采购项目对象都添加到Set 和然后将该集合添加到发票对象。)
但是,当我保存发票和销售单独时,两者都成功保存。(在一个会话中保存发票详细信息,提交其交易,然后再次将项目保存在另一个单独的会话中)。(休眠映射和创建实体使用 Netbeans IDE 完成)
请任何人告诉我问题出在哪里以及我应该在哪里检查。也让我知道如何在 netbeans IDE 中查看休眠 sql 执行。
父发票映射;
<?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>
子实体;销售
<?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.Selling" table="selling" catalog="ssm">
<composite-id name="id" class="Entity.SellingId">
<key-property name="invoiceInvNo" type="int">
<column name="Invoice_inv_no" />
</key-property>
<key-property name="itemItemid" type="string">
<column name="Item_itemid" length="12" />
</key-property>
</composite-id>
<many-to-one name="invoice" class="Entity.Invoice" update="false" insert="false" fetch="select">
<column name="Invoice_inv_no" not-null="true" />
</many-to-one>
<many-to-one name="item" class="Entity.Item" update="false" insert="false" fetch="select">
<column name="Item_itemid" length="12" not-null="true" />
</many-to-one>
<property name="sellQty" type="int">
<column name="sell_qty" not-null="true" />
</property>
<property name="soldPrice" type="double">
<column name="sold_price" precision="22" scale="0" not-null="true" />
</property>
</class>
</hibernate-mapping>
【问题讨论】:
-
能否贴出父子表的映射xml?
-
我将它们添加到我的问题中。感谢您的关注
-
我认为将 cascade="all" 添加到销售集将解决您的问题。请关注 Santosh 的回答并告诉我们。
-
再次感谢 Mak Ripon 先生的关注 :) 。问题出在映射中。现在用 Santosh 代码解决了它。 :)