File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes JPA mapping with inheritance, elementcollection and many-to-many relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA mapping with inheritance, elementcollection and many-to-many relationship" Watch "JPA mapping with inheritance, elementcollection and many-to-many relationship" New topic
Author

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

Kathleen Van Assche
Greenhorn

Joined: Jan 09, 2009
Posts: 1
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.





James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
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.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA mapping with inheritance, elementcollection and many-to-many relationship
 
Similar Threads
why referencedColumnName element missing in Joined table startegy
mapping problem and joinColumns
SecondaryTable Mapping
Simple But Crazy Cascade Problem
difference between joinColumn and PrimaryKeyJoinColumni