yasgur99
yasgur99

Reputation: 858

Stop recursion from bidirectional one to one mappings

I have a UserProfile Object that contains a reference to a TwitchAccount Object via a one to one relationship as shown here:

TwitchAccount.java:

@OneToOne(mappedBy = "twitchAccount")
private UserProfile profile;

UserProfile.java:

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "twitchAccountId", referencedColumnName = "twitchAccountId")
private TwitchAccount twitchAccount;

When I do a GET request on a specific TwitchAccount object, there seems to be a recursion between the two as seen:

{"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":

How can I stop this recursion from happening, and how can i make it so the twitch account doesn't even return the json associated from the user profile object (the twitch account object shouldn't be able to set or get the userprofile anyways)?

Upvotes: 0

Views: 91

Answers (1)

Christos Karapapas
Christos Karapapas

Reputation: 1086

You could just annotate the profile property with @JsonIgnore, like this,

@JsonIgnore
@OneToOne(mappedBy = "twitchAccount")
private UserProfile profile;

But this is certainly not a good practice, because this way you keep exposing entities which is a mistake and also because you would probably have a problem when you would need to expose the UserProfile.

So, create DTOs for both entities and on their relevant Services create conversion methods and it's done, you are in control of what you set.


However, this problem could be a bit more serious and to be more exact it could be a problem with your database tables and the relation between profile and account, but since you are not posting the rest of the code, there is no way to tell.

Just make sure to clear up what profile-account relation you need, because you might have created a bidirectional relation during database design instead of a unidirectional which is what, I guess, you are trying to accomplish.

Upvotes: 1

Related Questions