When creating a backend API, it's common to work with entity relationships to organize data. Typically, in courses or tutorials, we mostly see bidirectional relationships. But what if you want one entity to exist independently of the other? In this article, we’ll explore how to use a unidirectional relationship with JPA/Hibernate to achieve this.
Imagine you have two entities: Student and ThesisSchedule. The relationship between Student and ThesisSchedule is "many-to-one," meaning that a student can be associated with a thesis schedule, and each schedule can include multiple students.
In this case, our goal is to allow the creation of a Student without requiring a ThesisSchedule to be defined first. This independence is helpful, for instance, when adding students to the database before creating thesis schedules.
We’ll create Student and ThesisSchedule classes using a unidirectional "many-to-one" relationship from Student to ThesisSchedule.
Student entity code:
ThesisShedule entity code:
Here, we have a unidirectional relationship from Student to ThesisSchedule, indicated by the @ManyToOne annotation in the Student class. By specifying nullable = true, we allow a Student to be created without necessarily being associated with a ThesisSchedule.
Let’s see how this setup translates to the database and how data can be saved through an API.
With this setup, we can create a student without providing a ThesisSchedule.
POST request to create a Student (without ThesisSchedule):
This creates a new entry in the Student table with a null value for the thesis_schedule_id column.
Result:
Once a ThesisSchedule is created, we can update the Student record to associate with it.
Creating a ThesisSchedule:
This newly created ThesisSchedule might have an ID of 1.
Updating the student with ThesisSchedule:
Result:
Now, Larose is associated with the newly created ThesisSchedule.
In some cases, it may be more appropriate to manage the relationship from the ThesisSchedule side. This approach is useful if we want the thesis schedule to manage its associated students, keeping track of those participating in a specific schedule.
In this setup, ThesisSchedule holds a collection of Student to represent a "one-to-many" relationship, while Student doesn’t maintain a reference to ThesisSchedule.
ThesisSchedule entity code:
Student entity code:
In this configuration, ThesisSchedule contains a list of Student through the @OneToMany annotation. Consequently, students can be added or removed from ThesisSchedule without requiring a direct link in Student.
In conclusion, whether to manage the relationship on the Student or ThesisSchedule side depends on your application's specific needs:
Both configurations provide flexibility and allow for well-organized backend APIs based on the desired data relationships. By applying best practices to structure entity relationships, you can effectively model your database to meet your application’s specific needs.
Unidirectional relationships are a powerful option for managing optional dependencies between entities in a backend API.
I hope this solution helps other developers better understand and use unidirectional relationships in JPA/Hibernate.
The above is the detailed content of Relationships in JPA: Creating Entities Without Dependency. For more information, please follow other related articles on the PHP Chinese website!