Reputation: 3169
I have a Story containing a list of Comment, mapped with a JoinTable. I noticed that every time I add a new Comment to the list, hibernate deletes and recreates all entries in the join table related to the story. I'd expect it to just add a new row to the table. In the code below, the save methods are implemented by Spring Data. Am I missing something? Thank you.
Story.java:
@Entity
public class Story implements Serializable {
@OneToMany
@JoinTable(name="UserComment", joinColumns = @JoinColumn(name = "Story_id"), inverseJoinColumns = @JoinColumn(name = "Comment_id"))
private List<Comment> userComments;
...
Comment.java:
@Entity
public class Comment implements Serializable {
...
Adding a new Comment:
Comment comment = new Comment();
comment.setContent(content);
commentRepository.save(comment);
story.getUserComments().add(comment);
storyRepository.save(story);
Hibernate log on the storyRepository.save(story) execution:
Hibernate: delete from UserComment where Story_id=?
Hibernate: insert into UserComment (Story_id, Comment_id) values (?, ?)
Hibernate: insert into UserComment (Story_id, Comment_id) values (?, ?)
Hibernate: insert into UserComment (Story_id, Comment_id) values (?, ?)
Hibernate: insert into UserComment (Story_id, Comment_id) values (?, ?)
Hibernate: insert into UserComment (Story_id, Comment_id) values (?, ?)
Hibernate: insert into UserComment (Story_id, Comment_id) values (?, ?)
Hibernate: insert into UserComment (Story_id, Comment_id) values (?, ?)
Library versions:
Upvotes: 1
Views: 1080
Reputation: 153780
That's the expected behavior for a unidirectional bag used. According to [Hibernate Anti-Patterns][1]:
The bag semantics has the worst performance when it comes to the number of operations since it always re-creates the entire collection. Hibernate issues a delete statement to remove all associations of the old collection from the association table. Then, it issues N inserts to add all associations representing the new collection to the association table. Hibernate does not analyze how many elements have been changed in the collection.
Upvotes: 4