我有一个UserProfile对象,其中包含通过一对一关系对TwitchAccount对象的引用,如下所示:
TwitchAccount.java:
@OneToOne(mappedBy = "twitchAccount")
private UserProfile profile;
UserProfile.java:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "twitchAccountId", referencedColumnName = "twitchAccountId")
private TwitchAccount twitchAccount;
当我对特定的TwitchAccount对象执行GET请求时,似乎在两者之间存在递归:
{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":{"id":8,"firstName":null,"lastName":null,"profilePicUrl":null,"birthday":null,"steamId":null,"xblId":null,"psnId":null,"epicId":null,"discordId":null,"twitchAccount":{"id":1,"state":"525367bb-ca4c-4131-ac52-e29aafdb7dc1","code":null,"userProfile":
我该如何阻止这种递归的发生,以及如何做到这一点,以使抽搐帐户甚至不返回与用户个人资料对象相关联的json(抽搐帐户对象不应该设置或获取用户个人资料)?
答案 0 :(得分:1)
您可以像这样用@JsonIgnore注释profile
属性,
@JsonIgnore
@OneToOne(mappedBy = "twitchAccount")
private UserProfile profile;
但这当然不是一个好习惯,因为这样一来,您就不断暴露实体,这是一个错误,并且还因为当您需要公开UserProfile时可能会遇到问题。
因此,为这两个实体创建DTO,并在它们的相关服务上创建转换方法,完成后,您可以控制设置。
但是,此问题可能会更加严重,更确切地说,可能是数据库表以及配置文件和帐户之间的关系出现问题,但是由于您没有发布其余的代码,因此没有问题说话的方式。
只需确保清除您所需的个人资料-帐户关系,因为 您可能在数据库设计过程中创建了双向关系,而不是单向关系,而我想这就是您想要实现的。