부모 자식 형식 관계에 문제가 있습니다.
Hibernate docs는 부모로부터 외래 키 값을 얻기 위해 하위 클래스에 "다 대일"관계를 추가한다고 말합니다. 하지만이 관계가 작동하도록하려면 자식 클래스에 부모의 순환 포함을 도입하고 내 serializer를 손상시키는 Invoice 속성을 자식 클래스에 추가해야합니다. 누군가 내가 실수하고있는 부분을 지적 할 수 있습니까? 여기 최대 절전 모드로 부모/자식 관계 문제
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>
첫 번째 옵션을 시도했지만 오류가 발생했습니다 : " 'invoiceId'필드에 기본값이 없습니다." (BTW 외래 키 DB에서 notNull 및 nullable을 만들고 싶지 않아요). hibernate docs는 내가 피하려고하는 자식 클래스에 many-to-one 관계를 추가 할 것을 제안한다. – WSK
은 요소에 Invoice의 아래에 not-null = "true"를 추가한다. –
mdma
위대한입니다. 두 접근법 모두 작동합니다. 그들 중 누구도 다른 쪽보다 선호합니까? – WSK