Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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

 
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.



 
Bartender
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.
 
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
 
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!