One-to-Many Unidirectional Relationship
Learn how to implement one-to-many relationships and about the orphan removal attribute.
To show the one-to-many relationship, we will model the case where many players can register for a tournament. We will create a tournament table and a registration table to model this relationship.
Unidirectional one-to-many relationship means that only one side maintains the relationship details. So given a Tournament
entity, we can find the Registration
s but we cannot find the Tournament
details from a Registration
entity.
For the sake of organization, we will place all files for the one-to-many relationship example in a separate package. Create a new package named onetomany.uni
inside io.datajek.databaserelationships
. Copy the DatabaseRelationshipsApplication.java
file from the main package.
Creating entities
To model the one-to-many relationship, we will define a Tournament
class with three fields: id
, name
, and location
. The id
field is the primary key. We can also save other details like the dates in which the tournament takes place, the type of surface on which it will be played, and the number or rounds etc. The code of the Tournament
class with constructors and getter and setter methods is shown below:
@Entitypublic class Tournament {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private int id;private String name;private String location;public Tournament() {}public Tournament(String name, String location) {super();this.name = name;this.location = location;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}@Overridepublic String toString() {return "Tournament [id=" + id + ", name=" + name + ", location=" + location + "]";}}
Next, define the Registration
class with just one field, id
, for now. The id
field is the primary key for the table. We will add more fields later.
The Registration
class can store information about the registration date, the type of match (single/ doubles) for which the player registers, and the rank assigned to the player (seed) etc.
@Entitypublic class Registration {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private int id;public Registration() {}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "Registration [id=" + id + "]";}}
One to many relationship
Since a player registers for a tournament, a registration object should be associated with a player object.
We will update the Tournament
class to show the registrations. Since a tournament can have multiple registrations, we will add a List
of Registration
s as a new field.
public class Tournament {//...private List<Registration> registrations = new ArrayList<>();//...//update constructor, generate getter and setter methods , update toString()public Tournament(String name, String location, List<Registration> registrations) {super();this.name = name;this.location = location;this.registrations = registrations;}public List<Registration> getRegistrations() {return registrations;}public void setRegistrations(List<Registration> registrations) {this.registrations = registrations;}@Overridepublic String toString() {return "Tournament [id=" + id + ", name=" + name + ", location=" + location + ", registrations=" + registrations+ "]";}}
@OneToMany
The Tournament
class has a one-to-many relationship with the Registration
class as one tournament can have multiple registrations. This can be modelled by the @OneToMany
annotation. In a one-to-many relationship, the primary key ...