更改 parent 集 JPA 2 + Hibernate 5 后,孤儿不会被删除
orphans are not getting deleted once changing the parent set JPA 2 + Hibernate 5
我可能做错了什么,但我不知道是什么。
我有 2 个 table 和 2 个具有以下映射的实体
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "parent")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@JsonIgnore
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@Getter
private List<ChildBase> childs;
public Parent(){
this.childs = new ArrayList ();
}
@Entity
@Table(name = "childs")
@Getter
@Setter
@ToString
@Accessors(chain = true)
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class ChildBase {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
@JoinColumn(nullable = false)
@ManyToOne
@ToString.Exclude
protected Parent parent;
public ChildBase(Parent parent){
this.parent = parent;
}
}
@Entity
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Getter
@Setter
@Accessors(chain = true)
public class MyChild extends ChildBase {
@Enumerated(EnumType.STRING)
@Column(name = "name")
private String name;
public MyChild(String name, Parent parent) {
super(parent);
this.name = name;
}
一旦我尝试通过以下测试断开 parent 与 child 的连接,问题就会出现:
@Test
public void test(){
Parent parent = new Parent();
MyChild child1 = new MyChild("a", parent);
MyChild child2 = new MyChild("a", parent);
parent.getChilds.add(child1);
parentRepository.save(parent);
parent.getChilds.remove(child1);
parent.getChilds.add(child2);
parentRepository.save(parent);
}
我希望只看到“childs”table 中的第二 child 行,但我看到了它们。
编辑:
我也试过在第二次保存之前从 child 引用 null,如下所示:child1.setParent(null)。
也无济于事
这是双向关系,MyChild
分别是 ChildBase
是关系的拥有方。
要触发孤儿删除,您也必须 child1.setParent(null)
。
更新:进行了聊天提供的设置。这是 类,它工作得很好。
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "parent")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@JsonIgnore
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@Getter
private List<ChildBase> childs;
public Parent(){
this.childs = new ArrayList<>();
}
}
@Entity
@Table(name = "childs")
@Getter
@Setter
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Accessors(chain = true)
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class ChildBase {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@EqualsAndHashCode.Include
protected long id;
@JoinColumn(nullable = false)
@ManyToOne
@ToString.Exclude
protected Parent parent;
public ChildBase(Parent parent){
this.parent = parent;
}
}
@Entity
@NoArgsConstructor
@Getter
@Setter
@Accessors(chain = true)
public class MyChild extends ChildBase {
@Column(name = "name")
private String name;
public MyChild(String name, Parent parent) {
super(parent);
this.name = name;
}
}
这是测试。执行后数据库中只有childB
.
@Test
void test() {
Parent parent = new Parent();
MyChild child1 = new MyChild("childA", parent);
MyChild child2 = new MyChild("childB", parent);
parent.getChilds().add(child1);
parentRepository.save(parent);
parent.getChilds().remove(child1); // first remove
child1.setParent(null); // set parent null
parent.getChilds().add(child2);
parentRepository.save(parent);
}
我可能做错了什么,但我不知道是什么。 我有 2 个 table 和 2 个具有以下映射的实体
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "parent")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@JsonIgnore
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@Getter
private List<ChildBase> childs;
public Parent(){
this.childs = new ArrayList ();
}
@Entity
@Table(name = "childs")
@Getter
@Setter
@ToString
@Accessors(chain = true)
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class ChildBase {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
@JoinColumn(nullable = false)
@ManyToOne
@ToString.Exclude
protected Parent parent;
public ChildBase(Parent parent){
this.parent = parent;
}
}
@Entity
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Getter
@Setter
@Accessors(chain = true)
public class MyChild extends ChildBase {
@Enumerated(EnumType.STRING)
@Column(name = "name")
private String name;
public MyChild(String name, Parent parent) {
super(parent);
this.name = name;
}
一旦我尝试通过以下测试断开 parent 与 child 的连接,问题就会出现:
@Test
public void test(){
Parent parent = new Parent();
MyChild child1 = new MyChild("a", parent);
MyChild child2 = new MyChild("a", parent);
parent.getChilds.add(child1);
parentRepository.save(parent);
parent.getChilds.remove(child1);
parent.getChilds.add(child2);
parentRepository.save(parent);
}
我希望只看到“childs”table 中的第二 child 行,但我看到了它们。
编辑:
我也试过在第二次保存之前从 child 引用 null,如下所示:child1.setParent(null)。 也无济于事
这是双向关系,MyChild
分别是 ChildBase
是关系的拥有方。
要触发孤儿删除,您也必须 child1.setParent(null)
。
更新:进行了聊天提供的设置。这是 类,它工作得很好。
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "parent")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@JsonIgnore
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@Getter
private List<ChildBase> childs;
public Parent(){
this.childs = new ArrayList<>();
}
}
@Entity
@Table(name = "childs")
@Getter
@Setter
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Accessors(chain = true)
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class ChildBase {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@EqualsAndHashCode.Include
protected long id;
@JoinColumn(nullable = false)
@ManyToOne
@ToString.Exclude
protected Parent parent;
public ChildBase(Parent parent){
this.parent = parent;
}
}
@Entity
@NoArgsConstructor
@Getter
@Setter
@Accessors(chain = true)
public class MyChild extends ChildBase {
@Column(name = "name")
private String name;
public MyChild(String name, Parent parent) {
super(parent);
this.name = name;
}
}
这是测试。执行后数据库中只有childB
.
@Test
void test() {
Parent parent = new Parent();
MyChild child1 = new MyChild("childA", parent);
MyChild child2 = new MyChild("childB", parent);
parent.getChilds().add(child1);
parentRepository.save(parent);
parent.getChilds().remove(child1); // first remove
child1.setParent(null); // set parent null
parent.getChilds().add(child2);
parentRepository.save(parent);
}