Introduction to Spring Data JPA Part 7: Unidirectional Many-to-Many Relationships
Join the DZone community and get the full member experience.Join For Free
We will discuss the following:
- Unidirectional many-to-many relations.
- CRUD Operations.
Let us start by modeling the entities.
Let us see how Hibernate creates the tables for us.
A mapping table is created.
Since it is a Many-to-Many, Unidirectional relationship, the definition in the User is as follows, and the role entity will not have any definition.
The only thing that has to be noted is the definition of the relation in the User Entity.
@ManyToMany(targetEntity = Role.class,cascade = CascadeType.ALL)
private List<Role> roles;
Let us see how it translates to code. All CRUD operations are done in the following code. We will modify the UPDATE methods in the next article. The code has been explained in the previous articles. I have attached a video tutorial too for a detailed explanation of the code.
We will modify the update method in the next article.
Please find the source code at https://github.com/gudpick/jpa-demo/tree/many-to-many-unidirecctional-starter
Please find the video tutorials
Opinions expressed by DZone contributors are their own.