Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of endless loop when querying data using @ ManyToMany

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to solve the problem of endless loops when querying data using @ ManyToMany". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the problem of endless loop when querying data using @ ManyToMany.

An endless loop when querying data using @ ManyToMany

Beginners to use spring data jpa to record problems

Take User and Role as an example, they have a two-way many-to-many relationship, that is, Role information can be queried through User or User information can be queried through Role.

First of all, we have to understand why there is an endless loop. The reason for this endless loop is that when querying User, the Role attribute is included, and the Role needs to query except the user attribute. This is not the problem of spring data jpa, but as long as the code is related to each other will cause this situation. I have probably developed two ways to solve this situation.

First, add @ JsonIgnore annotation to Role

The code is as follows

User.java

Package com.example.demo.entity;import lombok.Data; import javax.persistence.*;import java.util.Set; / * * @ author lidai * @ date, 2018-10-23 13:53 * / @ Entity@Data@Table (name = "t_user") public class User {@ Id private String userId; private String username; private String password @ ManyToMany (cascade = CascadeType.ALL, fetch=FetchType.EAGER) @ JoinTable (name = "t_user_role", joinColumns = @ JoinColumn (name = "user_id", referencedColumnName = "user_id"), inverseJoinColumns = @ JoinColumn (name = "role_id", referencedColumnName = "role_id") private Set roleSet;}

Role.java

Package com.example.demo.entity; import com.fasterxml.jackson.annotation.JsonIgnore;import lombok.Data; import javax.persistence.*;import java.util.Set; / * * @ author lidai * @ date, 2018-10-29 14:15 * / @ Entity@Table (name = "t_role") @ Data@EqualsAndHashCode (exclude = {"userSet"}) public class Role {@ Id @ GeneratedValue private String roleId; private String roleName; private String remark @ JsonIgnore @ ManyToMany (fetch = FetchType.LAZY,mappedBy = "roleSet") private Set userSet;}

Many beginners may have some doubts about the @ ManyToMany annotation. My understanding below is for reference only.

@ ManyToMany stands for many-to-many relationships

Cascade attribute

CascadeType.ALL: cascading contains all persistence methods

Only when class An is added to CascadeType.PERSIST, cascading B objects are added. Throw an exception if the B object is in the data inventory (with the new) (make B persistent)

CascadeType.MERGE refers to the addition or change of class A, cascading B objects (add or change)

When only class An of CascadeType.REMOVE is deleted, cascading deletion of class B will occur.

@ JoinTable associates an intermediate table, for example, the intermediate table between User and Role is t_user_role

Name: intermediate table name

JoinColumns: the foreign key associated with the first table in the middle table (the first table is the user table in user)

InverseJoinColumns: similar to JoinColumns, the foreign key associated with the second table

Fetch

FetchType.EAGER: load now

FetchType.LAZY: lazy load

MappedBy = "roleSet": the List attribute name in the roleSet several-bit User table

The above is the first solution

Change two-way association to one-way association

Delete the following code from the Role table

@ JsonIgnore @ ManyToMany (fetch = FetchType.LAZY,mappedBy = "roleSet") private Set userSet

However, when one-way association occurs, the information of User cannot be queried through Role.

The use and understanding of unidirectional many-to-many @ ManyToMany

One-way many-to-many: one entity class can get another entity class.

Many-to-many: an employee can have multiple roles, and a role can correspond to multiple employees

/ / role one-way many-to-many: configure intermediate table / / many-to-many: an employee can have multiple roles, and one role can correspond to multiple employees @ ManyToMany (fetch = FetchType.LAZY) / / configure lazy loading / / JoinTable is the name of the intermediate table, joinColumns specifies the field associated with its own ID in the intermediate table, joinColumn is the column name, and inverseJoinColumns represents the field associated with the other party's ID in the intermediate table. @ JoinTable (name = "employee_role", joinColumns = @ JoinColumn (name = "employee_id"), inverseJoinColumns = @ JoinColumn (name = "role_id") @ JsonIgnore / / generate json to ignore this attribute (most of the data is meaningless, and it is possible to cause an endless loop) / / set the role to have multiple roles that cannot be repeated private Set roles = new HashSet () At this point, I believe you have a better understanding of "how to solve the problem of endless loops when querying data using @ ManyToMany". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report