春数据JPA - 多对一无法删除子,而无需修改父列表

问题描述:

我挣扎了一个星期有以下问题:春数据JPA - 多对一无法删除子,而无需修改父列表

怎么可能通过一个存储库中删除子实体不修改对所属的名单(父母)方面的关系?

在此先感谢。

我希望得到一些答案!

孩子类:

@Entity 
@Table(name = "child") 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public class Child implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator") 
    @SequenceGenerator(name = "sequenceGenerator") 
    private Long id; 

    @ManyToOne 
    private Parent parent; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public Long getParent() { 
     return parent; 
    } 

    public void setParent(Parent parent) { 
     this.parent = parent; 
    } 
} 

而且类:

@Entity 
@Table(name = "parent") 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public class Parent implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator") 
    @SequenceGenerator(name = "sequenceGenerator") 
    private Long id; 

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) 
    @JsonIgnore 
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
    private Set<Child> children = new HashSet<>(); 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public Set<Child> getChildren() { 
     return children; 
    } 

    public void setChildren(Set<Child> children) { 
     this.children = children; 
    } 

    public Parent addChild(Child child) { 
     this.children.add(child); 
     child.setParent(this); 
     return this; 
    } 

    public Parent removeChild(Child child) { 
     this.children.remove(child); 
     child.setParent(null); 
     return this; 
    } 
} 

而这里测试

@Test 
@Transactional 
public void testParentToChildRelationShip() { 
    Parent parent = new Parent(); 
    Child child = new Child(); 

    parent.addChild(child); 
    parent.addChild(new Child()); 
    parent.addChild(new Child()); 
    parent.addChild(new Child()); 

    parentRepository.save(parent); 

    Assertions.assertThat(parentRepository.count()).isEqualTo(1L); 
    Assertions.assertThat(childRepository.count()).isEqualTo(4L); 

    childRepository.delete(child); 


    Assertions.assertThat(parentRepository.count()).isEqualTo(1L); 
    // fails 
    Assertions.assertThat(childRepository.count()).isEqualTo(3L); 

    parentRepository.delete(parent.getId()); 

    Assertions.assertThat(parentRepository.count()).isEqualTo(0L); 
    Assertions.assertThat(childRepository.count()).isEqualTo(0L); 
} 

如果我删除一个孩子之前将测试会的工作,

child.getParent().removeChild(child); 

,但我想避免调用此。 有没有办法让它只使用Child-JPA-Repository.delete方法?或者我错过了其他注释?

+0

如果冲洗(会发生什么)? –

+0

它没有区别。子储存库的数量仍然是4.我尝试冲洗子储存库,父储存库和两个 –

+0

如果从安装方法填充集合会发生什么 - txn范围是不同的? – farrellmr

由于childparent你都面临这个问题的关联,您需要删除小孩和父母或者使用

parent.removeChild(child); 

child.getParent().removeChild(child); 

从父类中删除这些线之间的联系并且还可以设置和获取children

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) 
     @JsonIgnore 
     @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
     private Set<Child> children = new HashSet<>(); 

我想你可以从你的parent class删除child mapping让您轻松删除使用ChildRepositorydelete()方法,但问题的子行是你必须使用ChildRepositorysave(),以节省您的孩子manually。您无法使用ParentRepository保存具有父对象的子对象。更改测试代码象下面这样调用childRepository.delete(小孩)后保存childparent

Parent parent = new Parent(); 
Parent parent = parentRepository.save(parent); 

    Child child = new Child(); 
    child.setParent(parent); 
    childRepository.save(child); 
+0

解决这个问题有没有其他的双向可能性? –

+0

我认为其他的解决方案应该是你可以在你的'child class'中创建一个字段'isDeleted',而不是试图删除子行,只是将这个字段'isDeleted'更新为'true',并且在得到'isDeleted'为'false'的行。 – CIPHER007