Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring @JsonManagedReference和@JsonBackReference无法从JsonBackReference模型获取关系数据_Spring_Spring Boot_Jpa_Spring Data Jpa_Jackson - Fatal编程技术网

Spring @JsonManagedReference和@JsonBackReference无法从JsonBackReference模型获取关系数据

Spring @JsonManagedReference和@JsonBackReference无法从JsonBackReference模型获取关系数据,spring,spring-boot,jpa,spring-data-jpa,jackson,Spring,Spring Boot,Jpa,Spring Data Jpa,Jackson,我与用户和角色有关系,其中角色与列“role\u id”有“一对多”关系,用户与列“role\u id”有“多对一”关系 当我在spring boot上使用relationship时,当我使用@OneToMany和@ManyToOne时,我得到了无限循环数据,所以我搜索并得到了@JsonManagedReference和@JsonBackRefence。这解决了我的有限循环问题 但是当我使用@JsonManagedReference(用于角色)和@jsonbackreference(用于用户)作

我与用户和角色有关系,其中角色与列“role\u id”有“一对多”关系,用户与列“role\u id”有“多对一”关系

当我在spring boot上使用relationship时,当我使用@OneToMany和@ManyToOne时,我得到了无限循环数据,所以我搜索并得到了@JsonManagedReference和@JsonBackRefence。这解决了我的有限循环问题

但是当我使用@JsonManagedReference(用于角色)和@jsonbackreference(用于用户)作为我想要的角色模型时。如下图所示:

但在用户模型中,并没有角色数据,比如这个屏幕截图

我想要的是,当我获得与我想要的相同的角色数据(1个屏幕截图)时,当我从用户处获得数据时,我想要如下屏幕截图所示的数据:

这是我的榜样:

public class Role {
/*
    another data
*/
   @JsonManagedReference(value = "user-role")
   @OneToMany(
           cascade = CascadeType.ALL,
           mappedBy = "role"
   )
   private List<User> users = new ArrayList<>();
}

我在stackoverflow和其他网站上搜索了很多次,但没有找到任何解决方案,你能帮我解决我的问题吗,非常感谢。

看起来你没有提供
@JsonManagedReference

尝试这样做:

@JsonManagedReference(value = "user-role")


要精确指定应管理的引用。

您需要以其他方式添加注释

@JsonManagedReference(for User) and @JsonBackRefence(for Role)
简言之,
@JsonBackRefence
将忽略曾经添加的内容

你可以参考这本书


或者在这里查看不同用例的列表。

这并不能解决我的问题,当我得到“用户”数据时,我有用户的“角色”数据,但当我得到“角色”数据时,我没有得到roleOhh的“用户”列表,所以你希望两种方式都能得到数据。至少用这种方法是不可能的。
@JsonBackReference(value = "user-role")
@JsonManagedReference(for User) and @JsonBackRefence(for Role)