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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JPA mapping with inheritance, elementcollection and many-to-many relationship

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an abstract class Syncable and three concrete classes Library, Book and Magazine. Syncable defines the primary key.

I want to map them to a MySQL database using JPA 2 (implementation eclipselink 2.2.0) and I'm using the joined strategy. The classes are deployed as a ejb-jar in an ear on GlassFish 3.1

The structure of the class Library, Book and Magazine are all very similar.

Now I want to add a List<String> attribute to Book mapped as a ElementCollection.

Here is goes strange, because the generated DDL script defines a BOOK_TAG table, but the BOOK_ID does not reference the BOOK_ID from the BOOK table, but the SYNC_ID from the SYNCABLE table. This means that in theory I could add tags to a magazine (database view), but this would not be possible according to the Java code.

The same happens when I add a many-to-many relationship between Library and Book. The generated table does not have foreign keys to the LIBRARY and BOOK table, but refers the SYNCABLE table.

Here also, it means that according to the database there exists a relationship between syncables, not only between library and book.

I understand that for each record in LIBRARY/BOOK/MAGAZINE there is a record in the SYNCABLE table and that the primary key is 'common' between the tables, but I certainly want to force the constraint to the right table and not to the SYNCABLE table. Is this possible? And how can I map this? Changing the structure of the java code is not an option, except adding annotations and Syncable must be queryable.

Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Id in JPA is defined in the root table, so all relationships generally refer back to it.
Note that the JoinColumn in JPA defaults to referencing the root table Id always, you could try to set the referencedColumnName to be the secondary table "BOOK.BOOK_ID", not sure if that would work.

If you want the database constraint to the secondary table, you could just define the constraint yourself to any SQL tool, your own DDL script, or just use a native SQL query to generate it.

Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic