This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have some 1..n relations and some 0..n relations between tables in my data model.
Is there a mechanism in JPA that allows to map such relations?
First example: table Person and table ID Card.
One Person can have many ID Cards, if he/she has more than one nationality.
One Person MUST have at leat one ID Card.
An ID Card MUST belong to exactly one Person - it cannot have more than one owner and cannot exist without owner at all.
Additlonally, I want it to implement in a way that ID Card 'knows' who owns it, and Person doesn't 'know' about ID Card.
So the relation is: Person (1) ------------------> (1..n) IDCard
Second example: table Person and table Passport.
The only difference is that a Person doesn't have to have a Passport.
So the relation is: Person (1) ------------------> (0..n) IDCard
How to make such distinction between 1..n and 0..n using JPA?
You can also add cascadeType options to deal with deletes, so associated objects get deleted all at once. And of course, most databases supply other options you can do at the admin level to enforce relationships between tables.