Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring,JPA-双向@OneToMany:用另一个替换子列表_Spring_Jpa_Spring Boot - Fatal编程技术网

Spring,JPA-双向@OneToMany:用另一个替换子列表

Spring,JPA-双向@OneToMany:用另一个替换子列表,spring,jpa,spring-boot,Spring,Jpa,Spring Boot,我读了很多关于这方面的文章,做了数百个实验,但到目前为止还没有成功。我有以下课程: class Parent { @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL/*, orphanRemoval=true*/) private List<Child> children = new ArrayList<>(); class Child { @ManyToOne(cascade =

我读了很多关于这方面的文章,做了数百个实验,但到目前为止还没有成功。我有以下课程:

class Parent {
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL/*, orphanRemoval=true*/)
    private List<Child> children = new ArrayList<>();

class Child {
    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "parentId", nullable = false)
    @JsonIgnore
    Parent parent;
}
行为如下:

  • 当我按原样运行它时,它会添加新的,但保留旧的 一个!所以我有越来越多的不想要的孩子(对不起…)
  • 当我 取消注释
    删除=true
    部分父级已删除
你能解释一下它为什么会这样,我在这里能做什么吗?

找到了解决方案

我应该有一个=真的:

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval=true)
现在,由于@ManyToOne中的另一个级联,它删除了父级。我把它改为:

@ManyToOne(cascade={CascadeType.MERGE})


现在它可以工作了。

因为这就是您配置它的目的。。。您删除了
孤立删除=true
。问题还在于,您将所有内容级联在一个循环中。从子对象的父字段中删除级联。谢谢,看起来我自己在同一时间发现了相同的问题:)
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval=true)