Reputation: 6256
I have @OneToOne
relationship between my entities DirigeantsEntreprise
and Fournisseur
like this :
@Entity
@Table(name = "dirigeants_entreprise", catalog = "ao")
public class DirigeantsEntreprise implements java.io.Serializable {
private int idEntreprise;
private Fournisseur fournisseur;
private String nom;
private String poste;
....
@Id
@Column(name = "id_entreprise", unique = true, nullable = false)
public int getIdEntreprise() {
return this.idEntreprise;
}
...
@MapsId
@OneToOne
@JoinColumn(name = "id_entreprise")
public Fournisseur getFournisseur() {
return this.fournisseur;
}
but when i try to save object :
....
fournisseur_respository.save(fournisseur);
dirigeants_repo.save( new DirigeantsEntreprise( fournisseur,...,... ));
i got this exception :
javax.persistence.EntityExistsException:
A different object with the same identifier value was already associated with
the session : [persistence.DirigeantsEntreprise#35]
PS : 35 is the ID of my fournisseur
I thinks the problem is with maps and i have two object of DirigeantsEntreprise
and Fournisseur
with same identifier 35
.
How to resolve this problem ?
Upvotes: 1
Views: 513
Reputation: 6256
We Have to flush the session by saveAndFlush()
so it will be just one object with same identifier.
....
fournisseur_respository.saveAndFlush(fournisseur);
dirigeants_repo.save( new DirigeantsEntreprise( fournisseur,...,... ));
Upvotes: 1