2016-02-25 53 views
2

我有一個子實體和兩個父實體,每個父實體都有一個子實體列表。如果我通過首先獲取Parent_1的子項然後將該列表分配給Parent_2,爲Parent_2創建子實體列表,則hibernate根據以下代碼刪除「Parent_1-Child」連接表記錄。休眠從相關連接表中刪除多個多對多關係的數據

Parent_1:

@Entity 
@Table 
public class Parent_1 extends BusinessObject { 

    List<Child> children = new ArrayList<Child>(); 

    public Parent_1() {} 

    public Parent_1(List<Child> children) { 
     this.children = children; 
    } 

    @ManyToMany(fetch = FetchType.EAGER) 
    @JoinTable(name = "parent1_children", joinColumns = @JoinColumn(name = "parent1_id"), inverseJoinColumns = @JoinColumn(name = "child_id")) 
    @OrderColumn(name = "child_order") 
    public List<Child> getChildren() { 
     return children; 
    } 

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

} 

Parent_2:

@Entity 
@Table 
public class Parent_2 extends BusinessObject { 

    List<Child> children = new ArrayList<Child>(); 

    public Parent_2() {} 

    public Parent_2(List<Child> children) { 
     this.children = children; 
    } 


    @ManyToMany(fetch = FetchType.EAGER) 
    @JoinTable(name = "parent2_children", joinColumns = @JoinColumn(name = "parent2_id"), inverseJoinColumns = @JoinColumn(name = "child_id")) 
    @OrderColumn(name = "child_order") 
    public List<Child> getChildren() { 
     return children; 
    } 

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

} 

兒童:

@Entity 
@Table 
public class Child extends BusinessObject { 

    public Child() {} 

} 

我運行代碼有錯誤發生:

@Test 
public void demoTest() { 
    Child child_1 = new Child(); 
    Child child_2 = new Child(); 
    save(child_1, child_2); 

    List<Child> children = new ArrayList<Child>(); 
    children.add(child_1); 
    children.add(child_2); 

    Parent_1 parent_1 = new Parent_1(children); 
    Parent_1 parent_1_1 = new Parent_1(children); 
    save(parent_1, parent_1_1); 

    Parent_1 existingParent = dataAccessService.getParentById(new Long(1)); 
    List<Child> newChildren = existingParent.getChildren(); 

    Parent_2 parent_2 = new Parent_2(newChildren); 
    save(parent_2); 
} 

這是SQL正在爲最後的挽救執行:

Hibernate: 
    insert 
    into 
     Parent_2 
     (created, deleted, hidden, lastModified, id) 
    values 
     (?, ?, ?, ?, ?) 
Hibernate: 
    delete 
    from 
     parent1_children 
    where 
     parent1_id=? 
Hibernate: 
    insert 
    into 
     parent2_children 
     (parent2_id, child_order, child_id) 
    values 
     (?, ?, ?) 
Hibernate: 
    insert 
    into 
     parent2_children 
     (parent2_id, child_order, child_id) 
    values 
     (?, ?, ?) 

問題是與DELETE語句。爲什麼hibernate執行這個操作,我應該怎麼做才能阻止它的發生?

RGDS, 菲爾

注:BusinessObject的只是證明每個表的ID按:

@Id 
@GeneratedValue(strategy = GenerationType.TABLE) 
public Long getId(){ 
    return id; 
} 
+0

那麼我可能會問,爲什麼你在子列表上有一個'ManyToMany'註解?我沒有看到任何孩子回到父母身邊。 –

+0

其實我只是限制了最初的例子,使它更易於閱讀。我添加了具有相同子項列表的第二個Parent_1的創建,以顯示對ManyToMany的需求。這個問題沒有什麼變化。 –

+0

「save」方法有哪些?另外,什麼是交易界限? –

回答

0

最好的答案我可以給你的是,它似乎在被工作確定爲JPA冬眠。我重新創建了你的課程。我將註釋放在字段而不是方法上,但我認爲這沒什麼區別。我在Wildfly 9.0.2.Final上將它作爲JPA運行。我看到您正在使用持久性API Spring。我沒有試圖重現,但我認爲你的問題是可重複的。

+0

100%重現性。上面的例子實際上是我實際實現的一個簡化版本,因爲我希望我在其他地方用我的實際代碼犯了一個錯誤,這個簡單的例子可以工作 - 但事實並非如此。是的,Spring持久性API。 –

+0

您可能不得不繞過Spring持久性API來解決問題。我建議你看看你是否可以從你的設置中獲得EntityManager,然後嘗試使用em.persist而不是spring.save。 –

+0

謝謝尼古拉斯 - 它幫助了我,因爲我意識到我需要尋找不同的解決方案(更改我的表格結構)。似乎沒有這樣一個不尋常的問題,但畢竟它可能並不常見。謝謝。 –