-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Need help associating two classes with composite id's
PostPosted: Mon Sep 22, 2008 10:40 pm 
Newbie

Joined: Wed May 28, 2008 12:06 pm
Posts: 3
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:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 9:54 am 
Newbie

Joined: Wed May 28, 2008 12:06 pm
Posts: 3
As info, I kept hacking away at this and finally got it to work. I may have some questions later on, but for now I think I'm making some progress.

Thanks to all who have looked at this post.

rlc


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.