有什么方法可以使用 spring 从多表中获取数据,而无需无限 json 格式?
is there any way to fetch data from many to many tables using spring without infinite json format?
用户实体
@Id@GeneratedValue(strategy = GenerationType.AUTO)
Integer id;
// ... more properties
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "User_Boards", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "board_id")})
Set < Board > user_board = new HashSet < >();
//getter and setter and constructors
董事会实体
@Entity
public class Board implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
String name;
int P_id;
@ManyToMany(mappedBy = "user_board" , fetch = FetchType.LAZY)
Set<User> users_of_board = new HashSet<>();
//getter and setter and constructors
当我尝试使用 findAll 方法获取数据时,我得到了无限的 json 对象。
例如,当我获取用户时,我在其中有一组板我有一组用户,在其中我有一组板...等等
如何获取用户的看板和看板及其用户?
您可以在不想获取链接对象的class中使用@JsonBackReference
。
所以,如果我没有误解的话,一个用户有很多看板,一个看板属于一个或多个用户,所以获取一个用户会得到无限递归。
所以进入 Board
class 你必须做:
@ManyToMany(mappedBy = "user_board" , fetch = FetchType.LAZY)
@JsonBackReference //<--- Add this
Set<User> users_of_board = new HashSet<>();
然后,对象将不会是无限的。
您也可以查看 this 文章。
用户实体
@Id@GeneratedValue(strategy = GenerationType.AUTO)
Integer id;
// ... more properties
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "User_Boards", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "board_id")})
Set < Board > user_board = new HashSet < >();
//getter and setter and constructors
董事会实体
@Entity
public class Board implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
String name;
int P_id;
@ManyToMany(mappedBy = "user_board" , fetch = FetchType.LAZY)
Set<User> users_of_board = new HashSet<>();
//getter and setter and constructors
当我尝试使用 findAll 方法获取数据时,我得到了无限的 json 对象。
例如,当我获取用户时,我在其中有一组板我有一组用户,在其中我有一组板...等等
如何获取用户的看板和看板及其用户?
您可以在不想获取链接对象的class中使用@JsonBackReference
。
所以,如果我没有误解的话,一个用户有很多看板,一个看板属于一个或多个用户,所以获取一个用户会得到无限递归。
所以进入 Board
class 你必须做:
@ManyToMany(mappedBy = "user_board" , fetch = FetchType.LAZY)
@JsonBackReference //<--- Add this
Set<User> users_of_board = new HashSet<>();
然后,对象将不会是无限的。
您也可以查看 this 文章。