• 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

1..n and 0..n mapping

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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?
 
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, from a Java perspective, of object A must have an object B, you can enforce that through a constructor:



You can do something like that. You can map the association using JPA annotations:



Mapping 1..n or One to Many Relationships with Hibernate

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.

So, you have LOTS of options!

-Cameron McKenzie
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!