这篇文章主要给大家介绍了关于spring boot中防止递归查询的两种方式,两种方式分别是在application.properties中配置和在entity中添加注解,都给出了详细的示例代码,需要的朋友们下面来一起看看吧。
本文主要给大家介绍了关于spring boot防止递归查询的相关内容,这只是一个小提醒,这里有两种方式,很简单,下面来看看详细的介绍:
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<>();
......
}
以上就是介绍防止递归查询的两种方式的详细内容。