Spring Boot 防止递归查询
2016-12-29 本文已影响445人
就怕是个demo
这只是一个小提醒
这里有两种方式,很简单
1、在application.properties中配置
#懒加载配置
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
2、在entity中添加注解
- 在关联对象上添加@JsonBackReference
- 在类上添加@JsonIgnoreProperties("roles"),括号中填写不需要查出的对象
@Entity
@Table(name = "users")
//@JsonIgnoreProperties("roles")
public class User implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private int id;
@Column
private String name;
@Column(name = "created_at")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createdAt;
@ManyToOne
@JoinColumn(name = "dep_id")
@JsonBackReference //防止关系对象的递归访问
private Department department;
@ManyToMany(cascade = {}, fetch = FetchType.EAGER)
@JoinTable(name = "user_role", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
@JsonBackReference
private List<Role> roles = new ArrayList<>();
......
}