父/子关系与休眠问题 [英] Problem with parent/child relation with hibernate
问题描述
我正面临父子类型关系的问题。
Hibernate文档说在子类中添加一个多对一关系以从父类获取外键的值。但为了使这种关系起作用,我必须在子类中添加Invoice属性,将父项的循环包含到子元素中并打破我的序列化程序。有人能指出我在哪里做错了吗?
以下是我的代码:
Invoice.java
public class Invoice implements Serializable {
private Long id;
私人日期invDate;
私人字符串客户;
private Set< InvoiceItem>项目;
... getters / setters ...
}
InvoiceItem.java
public class InvoiceItem实现Serializable {
private Long itemId;
私人长期产品ID;
private int数量;
私人双重价格;
私人发票发票; // ???????
... getters / setters ...
}
Invoice.hbm .xml
< class name =Invoicetable =Invoices>
< id name =idcolumn =IDtype =long>
< generator class =native/>
< / id>
< property name =invDatetype =timestamp/>
< property name =customertype =string/>
< set name =itemsinverse =truecascade =all-delete-orphan>
< key column =invoiceId/>
< / set>
< / class>
InvoiceItem.hbm.xml
< class name =InvoiceItemtable =InvoiceItems>
< id name =itemIdtype =longcolumn =id>
< generator class =native/>
< / id>
< property name =productIdtype =long/>
< property name =quantitytype =int/>
< property name =pricetype =double/>
< / class>
您不必对发票在InvoiceItem中,如果你删除了inverse =true属性。
然后,Hibernate将创建一个单独的映射表,而不是在InvoiceItem表中使用外键。
移除 inverse
属性,并从映射中删除InvoiceItem中的Invoice属性和相应的多对一
,你应该得到你想要的。另外,您可以将InvoiceItem中的Invoice引用标记为transient,并在反序列化过程中处理填充值:遍历发票中的Set of Items项,并设置 code>发票将每件物品的财产归入所有权发票。
I am facing a problem with parent child type relations.
Hibernate docs say to add a "many-to-one" relation in child class to get value of foreign key from parent. But to make this relation work I have to add Invoice property in child class that introduce a circular inclusion of parent into child and breaks my serializer. Could somebody point where am I doing mistake?
Here is my code:
Invoice.java
public class Invoice implements Serializable {
private Long id;
private Date invDate;
private String customer;
private Set<InvoiceItem> items;
... getters/setters ...
}
InvoiceItem.java
public class InvoiceItem implements Serializable {
private Long itemId;
private long productId;
private int quantity;
private double price;
private Invoice invoice; //???????
... getters/setters ...
}
Invoice.hbm.xml
<class name="Invoice" table="Invoices">
<id name="id" column="ID" type="long">
<generator class="native" />
</id>
<property name="invDate" type="timestamp" />
<property name="customer" type="string" />
<set name="items" inverse="true" cascade="all-delete-orphan">
<key column="invoiceId" />
<one-to-many class="InvoiceItem" />
</set>
</class>
InvoiceItem.hbm.xml
<class name="InvoiceItem" table="InvoiceItems">
<id name="itemId" type="long" column="id">
<generator class="native" />
</id>
<property name="productId" type="long" />
<property name="quantity" type="int" />
<property name="price" type="double" />
<many-to-one name="invoiceId" class="Invoice" not-null="true"/> <!--????????-->
</class>
You don't have to have a reference to Invoice in InvoiceItem if you remove the inverse="true" attribute. Hibernate will then create a separate mapping table rather than use a foreign key in the InvoiceItem table.
Remove the inverse
attribute on the InvoiceItem set, and also remove the Invoice property from InvoiceItem, and the corresponding many-to-one
in the mapping and you should get what you want.
Alternatively, you could mark the Invoice reference in InvoiceItem as transient, and handle populating the value during deserialization: iterate over the Set of Items in Invoice, and set the invoice
property on each item to the owning invoice.
这篇关于父/子关系与休眠问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!