3

Trying to run Spring-Boot JPA on MySQL and literally followed this tutorial here : https://spring.io/guides/gs/accessing-data-mysql/ All went well as expected.

However, moment I brought in a new Entity viz. Address of the User and established 1-to-1 unidirectional mapping, the Application starts to throw StackOverFlow error - even though the mapping is very basic that I have used without issues on App Servers (e.g GlassFish/Payara 5) several times.

Below is the mapping :

In User Entity

@OneToOne(mappedBy = "user", optional = false, cascade = CascadeType.ALL) private Address address; 

..the other side of the relationship i.e. in Address :

@OneToOne(fetch = FetchType.LAZY) @JoinColumn(name="USER_ID") private User user; 

Expected : A single line : {"id":1,"name":"First","email":"[email protected]","address":{"id":2,"line1":"Line1","postcode":"PST CD","city":"City","state":"State","country":"IN"

Actual : The above line being printed on console countless times this there is a StackOverflow Error.

1 Answer 1

5

If you mean "Infinite recursion (StackOverflowError)".

You might need to add @JsonIgnore to the mapping parameters(Above your onetoone relationship) to stop looping.

Sign up to request clarification or add additional context in comments.

3 Comments

But how does the @JsonIgnore magic works?? It works no doubt but not very intuitive!
I'm suspecting that the serializer trying to convert the object graph doesn't know that it is navigating through the same objects again and again. Thus yielding a stack overflow error.
user.address -> Instance of Address, Address.user -> Instance of User

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.