Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot JPA One-to-One mapping generates StackOverflow Error

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.

like image 658
Singam Avatar asked Nov 19 '25 23:11

Singam


1 Answers

If you mean "Infinite recursion (StackOverflowError)".

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

like image 100
Aswin Ravi Avatar answered Nov 23 '25 04:11

Aswin Ravi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!