在我的角色模型中,我有这个模型:
@OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
mappedBy = "role")
private Set<User> users = new HashSet<>();
在我的用户模型中,我有这个:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id", nullable = true)
private Role role;
因此,用户可以拥有1个角色,而角色可以拥有许多用户。但是,当我向用户添加角色并运行API时,JSON数组会将所有用户添加到一行中。我做错了什么?
样品:
[{&#34; ID&#34:4,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&# 34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34 ;: 12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用& #34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&# 34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34 ;: 1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户& #34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{& #34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34; :12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;角色&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34;:{& #34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34; :1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{ &#34; ID&#34;:12,&#34;角色&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34;:{& #34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34; :1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{ &#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34 ;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34 ;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34;:{ &#34; ID&#34;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34 ;:1,&#34;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34 ;用户&#34;:[{&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[ {&#34; ID&#34;:12,&#34;作用&#34; {&#34; ID&#34;:1,&#34;用户&#34;:[{...&#34 ;
我想要完成的是用户而不是&#34; role_id&#34;列a&#34;角色&#34;具有角色名称值的列作为其&#39;值。
所以喜欢
用户:John,Doe,1
变为
用户:John,Doe,Boss