2017-08-18 105 views
0

我掙扎了一個星期有以下問題:春數據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

回答

1

由於childparent你都面臨這個問題的關聯,您需要刪除小孩和父母或者使用

parent.removeChild(child); 

child.getParent().removeChild(child); 
0

從父類中刪除這些線之間的聯繫並且還可以設置和獲取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