wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes JPA composite key mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA composite key mapping" Watch "JPA composite key mapping" New topic
Author

JPA composite key mapping

Alaneus Kingingestingest
Greenhorn

Joined: Dec 17, 2008
Posts: 12
Hi everybody!

I need help with JPA (Toplink) mapping.

I have 3 classes: Products, ProductsDescription and ProductsDescriptionPK.

Products has 1 ID: products_id
ProductsDescription has 2 IDs: products_id and language_id which is in ProductsDescriptionPK, so ProductsDescription has EmbeddedId: ProductsDescriptionPK.

Since 1 product can have many descriptions in different languages,
I wish to bind @OneToMany Products with ProductsDescription by using
products_id, but I can't figure it out how to do it since ProductsDescription has composite key.
Basically I wish to have Collection<ProductsDescription>
in Products which are bindded with its ProductsDescription. Here are the following classes.



Thanks

[ December 17, 2008: Message edited by: Alaneus Kingingestingest ]
[ December 17, 2008: Message edited by: Alaneus Kingingestingest ]
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

Hello Alaneus,

Welcome to javaranch!

The JPA specification doesn't specify a compound primary key with a entity, i don't know with Toplink but with Hibernate Entity Manager you use this



Another thing that i wanna suggest, you're overusing annotations. For instance you can use @Basic and @Column but it's not necessary. It's not
necessary because @Column have an attribute nullable to add the not null
constraint to the Column at hand. I think that the annotation configurer
will do it's stuff faster with the least annotations as possible, if of course you can do it with less annotations,

Regards,
[ December 17, 2008: Message edited by: Angel Taveras ]
Bill Hayes
Greenhorn

Joined: Sep 24, 2007
Posts: 24
I am relatively new to this but have been able to successfully accomplish what you're talking about with my own tables.

In your products class you need this...
Bill Hayes
Greenhorn

Joined: Sep 24, 2007
Posts: 24
I am relatively new to this but have been able to successfully accomplish what you're talking about with my own tables in the past couple of weeks.

In your products class you need something like this...


In your ProductsDescription class you need something like this...


Again, please keep in mind that this is new to me also. What I've done here seems to work, but I can't guarantee that it is 100% correct, is not missing something or does not include something extra. Therefore I hope that someone who has more knowledge can take a look and correct as necessary.
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

Hi Bill, you're right about the mapping that you're proposing it will work 100%, but what Alaneus it's trying to achieve it's the same behavior with a composite primary key,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA composite key mapping
 
Similar Threads
org.hibernate.QueryException: could not resolve property: ObjectId of: test.Objtablee
OneToOne Relation Exception
Problems using GenerationType.IDENTITY for in-memory databases
Can a class be mapped to a table and be Embeddable at the same time?
ava.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to