[英]Hibernate, Spring. Multiple representations of the same entity are being merged
[英]Multiple representations of same entity in spring boot
我必須同時更新三個表。 當我從郵遞員那里命中時,所有表都被成功保存,但是當我嘗試更新時,它會引發catch異常,例如同一實體的多個表示形式。
EmployeeDetails
public class EmployeeDetails implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", unique = true, length = 11, nullable = false)
private Integer userId;
//other variable
@OneToOne(mappedBy="user",cascade = CascadeType.ALL)
private EmployeeAdditionalinfo userAdditionalInfo;
@OneToMany(mappedBy="user",cascade = CascadeType.ALL)
private Set<EducationDetails> educationDetail;
//getter and setter
}
EmployeeAdditionalinfo
public class EmployeeAdditionalinfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_additional_info_id", unique = true, length = 11, nullable = false)
private Integer userAdditionalInfoId;
//other variable
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "user_id")
private EmployeeDetails user;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "address_id")
private EmployeeAddress address;
//getter or setter }
教育細節
public class EducationDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "s_no", unique = true, length = 10, nullable = true)
private Integer sno;
//variable
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "user_id")
private EmployeeDetails user;
//getter and setter }
錯誤
org.springframework.dao.InvalidDataAccessApiUsageException:同一實體的多個表示形式[com.icore.Payroll.Attendance.Entity.EmployeeDetails#379]正在合並。 分離:[EmployeeDetails [userId = 379,userName =用戶名,firstName = Ganesh1,lastName = Babu,middleInitial = Middle,email = Email,password = passwo,......]]
我認為您必須僅在關系的一側配置級聯。
當前,由於EducationDetails
實體內部的級聯配置,您正在觸發從EmployeeDetails
到EducationDetails
集的所有類型的層疊,然后又返回到EmployeeDetails
。
這是有道理的,因為該錯誤表明我們對同一實體有兩種不同的表示(實例),一種在您要刷新的持久性上下文內,另一種在上一次級聯期間檢索。
您可以嘗試刪除最后一個配置,並最終手動對其進行管理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.