簡體   English   中英

Spring Boot中同一實體的多種表示

[英]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實體內部的級聯配置,您正在觸發從EmployeeDetailsEducationDetails集的所有類型的層疊,然后又返回到EmployeeDetails

這是有道理的,因為該錯誤表明我們對同一實體有兩種不同的表示(實例),一種在您要刷新的持久性上下文內,另一種在上一次級聯期間檢索。

您可以嘗試刪除最后一個配置,並最終手動對其進行管理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM