Issue
I have a problem ... that seems pretty usual, but actually i don't find solution. I feel that it is something stupid but i cna't find what.
I have one class Post
@Entity(name = "PersistencePostEntity")
@Table(name = "post")
@NamedEntityGraph(
name = "post-with-rating",
attributeNodes = {
@NamesAttributeNode("rating")
}
)
public class PostEntity extends AbstractAuditingEntity {
@OneToMany(mappedBy = "childPost", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@Where(clause = "archived='false'")
@JsonManagedReference
private List<PostRelationEntity> clusterParents = new ArrayList<>();
}
and a PostRelationEntity class
@Entity(name = "PersistencePostRelationEntity")
@Table(name = "post_relation")
public class PostRelationEntity {
@ManyToOne
@JoinColumn(name = "child_post_id")
@JsonBackReference
private PostEntity childPost;
}
(I've selected the interesting fields cause the class are pretty long)
I would like that when i initialize a post with in it a list of relations in the clusterParenst field (relations without childPost init) the relations are automatically persisted with the childPost id inited with the newly created post.
The OneToMany / ManyToOne relation should do it but it doesn't. Any suggestion ? I haven't do Spring since a few months and i'm not finding my problem.
Thank you by advance for your answers
Solution
I have finally found a solution, when i'm saving the post with the PostRepository, i'm also persisting manually the relations with their own Repo. This is simplyer and cleaner
Answered By - Benoit Bonavia