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.
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.