gpt4 book ai didi

Hibernate多对多关联删除错误

转载 作者:行者123 更新时间:2023-12-02 17:43:38 26 4
gpt4 key购买 nike

我有两个类:EmployeeAward,具有多对多关系。

当尝试删除时,我收到违反约束的错误。我浏览了所有帖子,但没有一个有帮助。

这是代码:

@Entity
@Table(name="TB_AWARD")
public class Award implements Serializable{


@Id @GeneratedValue
@Column(name="AWARD_ID")
private long awardId;

@ManyToMany(mappedBy="awards")
@NotFound(action=NotFoundAction.IGNORE)
private Collection<Employee> employee = new ArrayList<Employee>();

@Column(name="AWARD_TYPE")
private String awardType;

@Column(name="AWARD_DATE")
private Date awardDate;

@Column(name="AWARD_DETAILS")
@Lob
private String awardDetails;

@Column(name="REMARK")
private String remark;

@Entity
@Table(name="TB_EMPLOYEE")
public class Employee implements Serializable {

@Id @GeneratedValue
@Column(name="EMPLOYEE_ID")
private long employeeID;

@ManyToMany(cascade= CascadeType.ALL)
private Collection<Award> awards;

最佳答案

您的映射错误,请尝试如下:

@Entity
@Table(name="TB_AWARD")
public class Award implements Serializable{


@Id @GeneratedValue
@Column(name="AWARD_ID")
private long awardId;

@ManyToMany(targetEntity=Employee.class, cascade = CascadeType.ALL)
@JoinTable(name = "AWARD_EMPLOYEE", joinColumns = { @JoinColumn(name ="AWARD_ID")},
inverseJoinColumns = { @JoinColumn(name = "EMPLOYEE_ID") })
@NotFound(action=NotFoundAction.IGNORE)
private Collection<Employee> employees = new ArrayList<Employee>();

@Column(name="AWARD_TYPE")
private String awardType;

@Column(name="AWARD_DATE")
private Date awardDate;

@Column(name="AWARD_DETAILS")
@Lob
private String awardDetails;

@Column(name="REMARK")
private String remark;

@Entity
@Table(name="TB_EMPLOYEE")
public class Employee implements Serializable {

@Id @GeneratedValue
@Column(name="EMPLOYEE_ID")
private long employeeID;

@ManyToMany(
cascade = CascadeType.ALL,
mappedBy = "employees",
targetEntity = Award.class
)
private Collection<Award> awards; //create the getter for this guy

始终查看examples in the reference以前,它会让你的生活变得更加轻松。 ;)

关于Hibernate多对多关联删除错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11676637/

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