Hello all. I’m sure that the answer to this question is buried somewhere in the Hibernate Annotations guide, but I just can’t find it. If someone can kindly point me to the relevant sections in the manual, I will most certainly try to figure this out.
I have two tables, PURCHASE_ORDER and PURCHASE_ORD_ITEM. PURCHASE_ORDER has a composite key of PO_NUMBER and PO_VERSION, and PURCHASE_ORDER_ITEM has a composite key of FK_PO_NUMBER, FK_PO_ VERSION and PO_ITEM_INDEX The two tables have a relational tie on the columns PO_NUMBER and PO_SEQUENCE.
The problem I’m having is figuring out how to set up the OneToMany relationship between PURCHASE_ORDER and its associated PURCHASE_ORD_ITEM’s. Much of my confusion is due to the fact that I have composite keys in both my primary (PURCHASE_ORDER) and child (PURCHASE_ORDER_ITEM) tables. I cannot find an example anywhere in the documentation that addresses this scenario.
First, the Embeddable key class for PURCHASE_ORDER:
@Embeddable
public class PurchaseOrderPK implements Serializable {
private String poNumber;
private String version;
...
}
Next, the PurchaseOrder class that utilizes the PK class above:
@Entity
@Table(name = "PURCHASE_ORDER", schema = "TEST")
public class PurchaseOrder implements Serializable {
private PurchaseOrderPK purchaseOrderPK;
private List<PurchaseOrderItem> purchaseOrderItems;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "poNumber", column = @Column(name = "PO_NUMBER")),
@AttributeOverride(name = "version", column = @Column(name = "PO_VERSION")) })
public PurchaseOrderPK getPrimaryKey() {
return purchaseOrderPK;
}
public void setPrimaryKey(PurchaseOrderPK pk) {
purchaseOrderPK = pk;
}
...
@OneToMany
WHAT DO I PUT HERE TO CONNECT THE PURCHASE ORDER TO THE ITEMS?
}
Next, the Embeddable key class for PURCHASE_ORD_ITEM:
@Embeddable
public class PurchaseOrderItemPK implements Serializable {
private String poNumber;
private String version;
private int index;
...
}
Finally, the PURCHASE_ORD_ITEM class
@Embeddable
@Entity
@Table(name = "PURCHASE_ORDER_ITEM", schema = "TEST")
public class PurchaseOrderItem implements Serializable {
private PurchaseOrderItemPK purchaseOrderItemPk;
private PurchaseOrder purchaseOrder;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "poNumber", column = @Column(name = "FK_PO_NUMBER")),
@AttributeOverride(name = "version", column = @Column(name = "FK_PO_VERSION")),
@AttributeOverride(name = "index", column = @Column(name = "PO_ITEM_INDEX"))
})
public PurchaseOrderItemPK getPurchaseOrderItemPk() {
return purchaseOrderItemPk;
}
public void set PurchaseOrderItemPK(PurchaseOrderItemPK poPk) {
this. purchaseOrderItemPk = poPk;
}
...
@?????????
WHAT DO I PUT HERE TO CONNECT THE ITEMS BACK TO THE PURCHASE ORDER?
}
If someone could please jump in here and get me pointed in the right direction on how to set up the association between these two classes, I would be most grateful. Thanks, and God bless!!!
rlc
Code:
Code: