error contents
java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:675)
at java.lang.StringBuilder.append(StringBuilder.java:208)
at java.time.LocalDate.toString(LocalDate.java:2032)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at demo.model.User.toString(User.java:19)
...
The relationship between User and UserDetail is a OneToOne two-way relationship.
User
@Entity
@Data
public class User {
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private UserDetail userDetail;
UserDetail
@Entity
@Data
public class UserDetail implements Serializable {
@OneToOne
@JoinColumn
@MapsId
private User user;
I forgot that adding @Data in Lombok will also add `` `@ Getter / @ Setter``` and @ToString. .. ..
Where is the circulation happening? .. When User.toString is executed, DetailUser.toString is executed, and User.toString is further called in it to start circulation.
Avoid calling DetailUser when User.toString is executed
User
@Entity
@Data
public class User {
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
@ToString.Exclude //★★★ Define fields to exclude from toString
private UserDetail userDetail;
Recommended Posts