Java 休眠父级级联删除单向子级错误
我有一个非常简单的春季开机测试Java 休眠父级级联删除单向子级错误,java,hibernate,Java,Hibernate,我有一个非常简单的春季开机测试 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } @Bean public CommandLineRunner demo(ParentRepository parentRepo, ChildRepo
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public CommandLineRunner demo(ParentRepository parentRepo, ChildRepository childRepo) {
return (args) -> {
Parent parent = new Parent("Father");
parent = parentRepo.save(parent);
childRepo.save(new Child("Father", "Jack"));
for (Child child : childRepo.findAll()) {
System.out.println(child);
}
parentRepo.findById(parent.getName()).ifPresent(p -> {
System.out.println(p);
1. p.getChildren().clear();//update child set parent_name=null NULL not allowed for column "PARENT_NAME";
2. p.setChildren(null); //A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: springtest.Parent.children
parentRepo.save(p);
});
for (Parent p : parentRepo.findAll()) {
System.out.println(p);
}
};
}
}
母实体
@Entity
public class Parent {
@Id
@Column(name = "NAME", nullable = false)
private String name;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "PARENT_NAME")
private Set<Child> children;
protected Parent() {}
public Parent(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Set<Child> getChildren() {
return children;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
@Override
public String toString() {
return "Parent[name=" + name + ", children=" + children.size() + "]";
}
}
父存储库
public interface ParentRepository extends CrudRepository<Parent, String> {}
public interface ChildRepository extends CrudRepository<Child, String> {}
public接口ParentRepository扩展了crudepository{}
儿童知识库
public interface ParentRepository extends CrudRepository<Parent, String> {}
public interface ChildRepository extends CrudRepository<Child, String> {}
public interface ChildRepository扩展了crudepository{}
我想从父实体中删除子实体。链接正在使用单向,并且拥有的实体是父实体。我试着打电话给1getChildren().clear()
,但hibernate最终生成了一个update语句,该语句将parent\u name设置为null(而不是delete where parent\u name=“Father”),这违反了子表中不可为null的约束
然后我试着打电话给2号setChildren(null)
,这一次它给出了一个异常拥有实体实例不再引用带有cascade=“all delete orphan”的集合
如何解决上述问题以使子项删除工作正常?作为所有者,只有“父项”才能修改foraign键。所以我会从“Child”中删除parentName
所以我想这样改变它:
@Entity
public class Child {
@Id
@Column(name = "NAME", nullable = false)
private String name;
protected Child() {}
public Child(String name) {
this.name = name;
}
@Override
public String toString() {
return "Child[name=" + name + "]";
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public CommandLineRunner demo(ParentRepository parentRepo, ChildRepository childRepo) {
return (args) -> {
Parent parent = new Parent("Father");
parent.add(new Child("Jack"));
parent = parentRepo.save(parent); //the child is saved because of the cascading
for (Child child : childRepo.findAll()) {
System.out.println(child);
}
parentRepo.findById(parent.getName()).ifPresent(p -> {
System.out.println(p);
p.getChildren().clear();
parentRepo.save(p);
});
for (Parent p : parentRepo.findAll()) {
System.out.println(p);
}
};
}
}