![](/img/trans.png)
[英]Hibernate - fetch data from multple tables using many to one annotation
[英]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 对象。
例如,当我获取用户时,我在里面有一组板子,里面有一组用户,里面有一组板子……等等。
我怎样才能用他的板和板和它的用户来获取用户?
您可以在不想获取链接对象的类中使用@JsonBackReference
。
所以,如果我没有误解的话,一个用户有很多板,一个板属于一个或多个用户,所以获取一个用户你会得到无限递归。
因此,进入Board
类,您必须执行以下操作:
@ManyToMany(mappedBy = "user_board" , fetch = FetchType.LAZY)
@JsonBackReference //<--- Add this
Set<User> users_of_board = new HashSet<>();
然后,对象不会是无限的。
您也可以查看这篇文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.