我有两个使用Spring和Hibernate的实体
实体A:
@Entity
@Table(name = "A")
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "a")
private B b;在这里,我与实体B有一对一的关系,该关系的所有者是实体A。
实体B:
@Entity
public class B{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne
@JoinColumn(name = "a_ID")
private A a;保存实体不会有任何问题。实体B获取数据库中实体A的ID。但是当我删除实体A时,我也想删除属于A的实体B。
当我删除时,我得到了这个错误:
MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails我认为我在实体中的声明是正确的。这里的问题是什么?
编辑
当我检查实体B的表,特别是实体A的外键时,它显示Restricted on Update en on Delete
发布于 2017-10-17 18:22:15
在类A中将orphanRemoval属性设置为true
像这样:@OneToOne(mappedBy = "a", cascade = CascadeType.ALL, orphanRemoval = true)
https://stackoverflow.com/questions/46787747
复制相似问题