Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JPA composite key mapping

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 84
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic