我有两个实体ProfileGroup
和User
的映射,其中用户与ProfileGroups有一个@OneToMany
关系;
问题是我的要求的返回未在所有ProfileGroup的JSON要求中显示表ProfileGroup的外键fk_user。
个人资料组
public class ProfileGroup {
...
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="fk_user_creator")
@JsonBackReference
private User userCreator;
...
用户
public class User {
@OneToMany(mappedBy="userCreator")
@JsonManagedReference
private List<ProfileGroup> profileGroups;
资源类:
@GetMapping
public ResponseEntity<?> listAll(){
List<ProfileGroup> profileGroupsReturned = profileGroupRepository.findAll();
return !profileGroupsReturned.isEmpty() ? ResponseEntity.ok(profileGroupsReturned) : ResponseEntity.noContent().build();
结果JSON :(不要显示FK_USER)
[
{
"id": 1,
"name": "Calango Careta",
"description": "Cadê? cadê? Nós somos a Orquestra Camaleônica do Calango Careta!!!!!!!!",
"founded": "2015-04-12",
"registered": "2018-11-14",
"active": true
},
{
"id": 2,
"name": "Tropicaos",
"description": "",
"founded": "2018-01-20",
"registered": "2018-11-14",
"active": true
},
{
"id": 3,
"name": "Capivara Brass Band",
"description": "",
"founded": "2016-08-24",
"registered": "2018-11-14",
"active": false
}
]
有什么主意吗?怎么了?
谢谢!!
答案 0 :(得分:1)
问题 这就是JsonBackReference批注的作用,尽管由于您具有双向关系,如果删除它,则在尝试序列化对象时会遇到无限循环。
解决方案