File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes How do I have a child table when the parent has a composite key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How do I have a child table when the parent has a composite key" Watch "How do I have a child table when the parent has a composite key" New topic
Author

How do I have a child table when the parent has a composite key

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2051
    
  22

I am trying to model a set of tables that are very poorly designed. The problem is that I have a table with a composite key, and this table has some child tables. So, the tables look like this



There is an Instrument table with a composite PK(instrument id and batchID) and a Payment table with a composite PK(instrument ID, batch ID and paymentDate) There is FK relationship between Payment and Instrument on instrumentID and batchID. For an Instrument there can be multiple Payments

My first inclination is to change the table to add proper sequence generated IDs, but doing so would impact a lot of legacy code. I might be able to do it, but if I can't, I wanted to see if there are any options to model the table as is in JPA

What I have now is this



The problem is I can;t figure out how to put the Join annotations. I wanted to put OneToMany on Instrument.payments and ManytoOne on PaymentPK.instrument. However, how do I put multiple column names in the relationship?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

You can do something like:


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I have a child table when the parent has a composite key
 
Similar Threads
JPA composite keys problem
EJB 3: JPA versus Beginning EJB 3 App Dev
PK in aggregated entity with other non-key attributes?
Composite Key consisting of an Entity and a Property
Why is Hibernate creating a unique constraint when I don't want one?