In one-to-many relationship, It is suggested that I should create synchronize utilities methods like addPhone()
, removePhone()
.
In my CRUD app, if i know the id
of the owner-side entity, i can already use the enititymanager to add it like this:
public CommentDTO postComment(Integer threadID, String content, Integer userId) {
User user = entityManager.getReference(User.class, userId);
Thread thread = entityManager.getReference(Thread.class, threadID);
Comment comment = new Comment(content, thread, user);
commentRepository.save(comment); // or entityManager.persist(comment);
return commentRepository.getCommentById(comment.getId());
}
So if I already know ThreadID then I dont need to use synchronize utilities methods right?
Is there any difference if I do like this:
public CommentDTO postComment(Integer threadID, String content, Integer userId) {
User user = entityManager.getReference(User.class, userId);
Thread thread = entityManager.getReference(Thread.class, threadID);
Comment comment = new Comment(content, user);
thread.addComment(comment);
entityManager.persist(thread);
return commentRepository.getCommentById(comment.getId());
}