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>