gpt4 book ai didi

java - Hibernate子表不更新

转载 作者:行者123 更新时间:2023-12-01 17:34:57 25 4
gpt4 key购买 nike

我有3个mysql数据库表,分别称为“Invoice”、“Selling”、“Invoice”。ER图如下。这是一个用于计费应用程序的简单数据库。

enter image description here

发票表保存有关发票的数据,销售表保存为特定发票购买的商品。项目表保存有关每个项目的详细信息。采购完成后,会开具发票。这就是我使用 Hibernate.Hsession 保存到数据库的方法,Hsession 是我的 Sessionfactory 所在的位置并返回 session 。Date unavailableD = 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(sales); 的销售详细信息(发票表具有一对一的关系,并且所有采购项目对象都添加到 Set 中,然后该集合添加到发票对象中。)但是,当我分别保存发票和销售时,两者都保存成功。(在一个 session 中保存发票详细信息,提交其交易,然后再次将项目保存在另一个单独的 session 中)。(Hibernate 映射和创建实体是使用 Netbeans IDE 完成的)

请任何人告诉我问题出在哪里以及我应该检查哪里。另外让我知道如何在 netbeans IDE 中查看 hibernate 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>

最佳答案

在 Invoice 实体的 Hibernate 映射中,您需要将级联类型设置为“persist”或可能为“all”,具体取决于您的需要。请参阅 Hibernate 文档 http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html

关于java - Hibernate子表不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com