As stated in the comments, you should take care of the consistency of the object graph with the child / parent relationship. This consistency will not be free when JSON comes directly from the POST request.
You must annotate the parent and child fields using @JsonBackReference and @JsonManagedReference .
Parent class:
@OneToMany(mappedBy = "parentId") @JsonBackReference private Collection<Child> childCollection;
Child class:
@JoinColumn(name = "parent_id", referencedColumnName = "parent_id") @ManyToOne(optional=false) @JsonManagedReference private Parent parent;
A similar question with an answer here
Also, if you use @JsonBackReference / @JsonManagedReference in javax.persistence annotated classes combined with Lombok @ToString annotation, you will get a stackoverflow error.
Just exclude the childCollection and parent field from the @ToString annotation with @ToString( exclude = ...)
The same will happen with the Lombok-created equals() method ( @Data , @EqualsAndHashCode ). Just implement these methods manually or use only @Getter and @Setter .
s17t.net
source share