• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

custom join table

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a many to many unidirectional relationship that uses a join table to link orders to books in a simple book store test application. the mapping looks like this:



this creates a join table called orders_books. but now i want to have a way to specify quantity. in this scheme a book can be ordered only once. i guess i could change it to be a list instead of a set but that seems redundant. what i want is to add another column in the orders_books table to specify the quantity. how can i accomplish this with the java persistence api?
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will need to create a Java class to model your BookOrder and add the quantity to it. You will then have a @OneToMany from Order to BookOrder and a @ManyToOne from BookOrder to Order and to Book.

You can either use a composite id for the BookOrder using both the foreign key fields, or add a generated id. I would recommend adding a generated id as it is simpler. If you decide to go with the composite id you will need to add duplicate basic @Id attributes for the foreign keys as JPA does not allow @ManyToOne to be used as an @Id.
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic