...

/

One-to-Many Bidirectional Relationship

One-to-Many Bidirectional Relationship

Learn how to create a one-to-many relationship.

In this lesson we will create a bidirectional one-to-many relationship where a Player can have many Registrations.

Let’s add some real life constraints to the model.

  • Every Registration object must be associated with a Player object.

  • When a Registration object is deleted, the associated Player object should not be deleted.

A bidirectional association between Player and Registration means that we can get all the Registration objects if we have a Player object and vice versa, we can get a Player by using the Registration. Compare this to the unidirectional one-to-many relationship, where we could find the Registration objects given a Tournament but we could not find the Tournament from a Registration object.

Press + to interact
Unidirectional relationship between tournament and registration
Unidirectional relationship between tournament and registration

The inverse of one-to-many relationship is many-to-one, where many registrations map to one player.

Press + to interact
Many to One relationship: many registrations map to one player
Many to One relationship: many registrations map to one player

For the bidirectional relationship example, create a new package in io.datajek.databaserelationships.onetomany. We will call it bi. Copy the following classes from the onetomany.uni package:

  • DatabaseRelationshipsApplication.java

  • Registration ...

Access this course and 1400+ top-rated courses and projects.