I encountered a strange problem while trying to save objects into Database.
I have a collection like this.
Code:
@OneToMany (fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy ="article")
private List<Element> elements = new ArrayList<>();
And for the sake of bidirectional relationship, in
Quote:
Element
class :
Code:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn (name="i_article")
protected Article article;
And this is my saving method :
Code:
tx = session.beginTransaction();
session.merge(art);
session.flush();
tx.commit();
The problem is, when i tried to save a new instance, Hibernate saves that instance twice in the database,
one with Collection,
one without the Collection. I tried
Quote:
saveAndUpdate
and its still the same. When i tried to edit , as well as add elements to an existing instance, it works well.
Can anyone explain this ?