6
I have a map as follows:
public class ClasseA {
public ClasseA(){
listaClasseB = new ArrayList<ClasseB>;
}
@OneToMany(mappedBy = "xxxx", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<ClasseB> listaClasseB;
}
public class ClasseB {
@ManyToOne
@JoinColumn(name = "xxxxxxxxxxx", nullable = false)
private ClasseA objetoClasseA;
}
In the method of the rescue service:
objetoClasseA.getListaClasseB().add(objetoClasseB);
And is made the persist or the merge only of objetoClasseA
.
When adding a objetoClasseB
, two records of the same are saved, even with the list containing only 1 element.
What reasons could lead to duplicate records?
I put the way the mapping is done in class B. There is no use of Scade. I believe not the case of objects Detached, as I am testing only with the empty list, adding a single
objetoClasseB
. What I’ve just noticed is that by persistingobjetoClasseA
, containing aobjetoClasseB
, the problem does not occur. When merging in the same situation, the problem manifests itself. There is some problem in instantiating the list in the Classea constructor ?– ewerton
So I recommend that you set the Scades correctly. Using the default values can cause this kind of problem.
– Edgar Muniz Berlinck
The Scade merge was the problem. I only left persist and delete. What I don’t understand is why the merge saves duplicate records and persist does not.
– ewerton
@user1902062 slightly improved my answer.
– Edgar Muniz Berlinck