Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA with onetomany between two tables, each having compound primary keys

 
Phil Cuccatti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please provide a working example of how to map with JPA, the following situation. One to many between two tables each having compound primary keys as follows:

We have searched everywhere and cannot find any examples using compound keys in both tables.

Here is what I have thus far.



 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the Javaranch.

Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I've added them for you since you are new and so that you can see the difference.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code looks fine. What issue are you having?

You could also use an IdClass instead of the EmbeddedId, this would let you put the @Id on the ManyToOne and avoid the duplicate mapping of the foreign keys.
 
Phil Cuccatti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am receiving the following error:

Initial SessionFactory creation failed.org.hibernate.MappingException: Unable to find column with logical name: SUPPLIER_PART_1_ID_PK1 in org.hibernate.mapping.Table(CARE.PRODUCTS_STUPID) and its related supertables and secondary tables
 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil,

This is what I read in API for JoinTable.


This annotation is used in the mapping of associations. It is specified on the owning side of a many-to-many association, or in a unidirectional one-to-many association.


But, your mapping is bi-directional. I think, JoinColumns must be used to define composite PK mapping.

I am no expert in Hibernate. Expert opinion welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic