I am not able to understand this statement from the book "EJB3 in Action" - Page 280, which says:
JPA does not support unidirectional one to many relationships using a foreign key on the target table and you cannot use the following mapping if you have a unidirectional one to many relationship between Item and Bid:
Hi , Unidirectional OneToMany relationships are not directly supported by JPA however they can be done by the use of a @JoinTable (similar to ManyToMany) I think its best you read this part from the specs .
Thank you. This was pretty enlightening for me as there's no such clarification in O'Reilly's book.
For the ones that might be interested, I am copying the relevant extract from the specs (section 9.1.24 OneToMany Annotation):
The default schema-level mapping for unidirectional one-to-many relationships uses a join table, as described in Section 18.104.22.168. Unidirectional one-to-many relationships may be implemented using one-to-many foreign key mappings, however, such support is not required in this release. Applications that want to use a foreign key mapping strategy for one-to-many relationships should make these relationships bidirectional to ensure portability.
I am new to JPA and i have a question about the OneTOMany unidirectional mapping. Let we assume the following example two entities
Person (ID, NAME) and Car (ID, CAR_NAME, PER_ID) i.e the foreign key is in the target table.
Here is my relationship mapping inside the Person entity;
Note this is an unidirectional OneToMany mapping from Person to Car. I am using Eclipselink and the above mapping is always not working . Sometimes system throws the exception and says CAR_ID is not found.
If any of you came similar issue please share your idea. Much appreciate your reply.
As said above, unidirectional OneToMany can not use foreign key mapping in current JPA. Instead a join table should be used. So the tables should be Person(ID, NAME), Car(ID, CAR_NAME) and Person_Car(PERSON_ID, CAR_ID).