I am trying to implement the soft delete on a table and below is the code I have done to achieve the same
@Entity
@Data
@Table(name = "users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String surname;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Product> products = new ArrayList<>();
}
I am getting the below exception on debugging
Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate com.test.User.toString()
please advise it is related to the impact of Lombok that I am using in the entity.
Lombok @Data generates toString for you and you are probably using bidirectional association in your Product class. And probably Product also has toString method overriden. You are simply getting infinite loop of toString calls because of this bidirectional association I guess.
One solution suggested by lealceldeiro would be to exclude products field in your User class from being taken into account for toString method generation. You can achieve this by annotating your field with @ToString.Exclude lombok annotation :
@ToString.Exclude
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Product> products = new ArrayList<>();
Or you could exclude the user field in your Product entity.
You can read about lombok @ToString on their official site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With