gpt4 book ai didi

mysql - @MapsId 以及与 session 关联的具有相同标识符值的两个对象

转载 作者:行者123 更新时间:2023-11-29 22:52:09 24 4
gpt4 key购买 nike

我的实体 DirigeantsEntrepriseFournisseur 之间有 @OneToOne 关系,如下所示:

@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;
}

但是当我尝试保存对象时:

....
fournisseur_respository.save(fournisseur);
dirigeants_repo.save( new DirigeantsEntreprise( fournisseur,...,... ));

我遇到了这个异常:

javax.persistence.EntityExistsException: 
A different object with the same identifier value was already associated with
the session : [persistence.DirigeantsEntreprise#35]

PS:35是我的fournisseur的ID

我认为问题出在 map 上,我有两个具有相同标识符 35DirigeantsEntrepriseFournisseur 对象。

如何解决这个问题?

最佳答案

我们必须通过 saveAndFlush() 刷新 session ,这样它将只是一个具有相同标识符的对象。

....
fournisseur_respository.saveAndFlush(fournisseur);
dirigeants_repo.save( new DirigeantsEntreprise( fournisseur,...,... ));

关于mysql - @MapsId 以及与 session 关联的具有相同标识符值的两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28859257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com