2015-05-12 95 views
0

我已經有一個維護項目的有序列表中的父實體:@OrderColumn映射 - 刪除列表中的第一個元素錯誤

@Entity 
public class ActionList { 

    private String id; 
    private String actionId; 
    private String name; 
    private List<ActionItem> items = new ArrayList<ActionItem>(); 

    @Id 
    public String getId() { 
     return this.id; 
    } 

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

    @Column(nullable = false) 
    public String getActionId() { 
     return this.actionId; 
    } 

    public void setActionId(final String actionId) { 
     this.actionId = actionId; 
    } 

    @Column(nullable = false) 
    public String getName() { 
     return this.name; 
    } 

    public void setName(final String name) { 
     this.name = name; 
    } 

    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "actionlist") 
    @OrderColumn(name = "position") 
    public List<ActionItem> getItems() { 
     return this.items; 
    } 

    public void setItems(final List<ActionItem> items) { 
     this.items = items; 
    } 

    @Override 
    public boolean equals(final Object obj) { 

     if (obj == null) { 
      return false; 
     } 
     if (obj == this) { 
      return true; 
     } 
     if (!(obj instanceof ActionList)) { 
      return false; 
     } 
     final ActionList other = (ActionList) obj; 

     return getActionId().equals(other.getActionId()); 
    } 

    @Override 
    public int hashCode() { 
     return Objects.hash(getActionId()); 
    } 
} 

這對項目的類:

@Entity 
public class ActionItem { 

    private String id; 
    private ActionList actionList; 
    private int position = 0; 
    private String name; 

    @Id 
    public String getId() { 
     return this.id; 
    } 

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

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    @JoinColumn(name = "actionList_id", nullable = false) 
    public ActionList getActionList() { 
     return this.actionList; 
    } 

    public void setActionList(final ActionList actionList) { 
     this.actionList = actionList; 
    } 

    @Column(name = "position") 
    public Integer getPosition() { 
     return this.position; 
    } 

    public void setPosition(final Integer position) { 
     this.position = position; 
    } 

    public String getName() { 
     return this.name; 
    } 

    public void setName(final String name) { 
     this.name = name; 
    } 

    @Override 
    public boolean equals(final Object obj) { 

     if (obj == null) { 
      return false; 
     } 
     if (obj == this) { 
      return true; 
     } 
     if (!(obj instanceof ActionItem)) { 
      return false; 
     } 
     final ActionItem other = (ActionItem) obj; 

     if (StringUtils.isNotEmpty(getId()) && StringUtils.isNotEmpty(other.getId())) { 
      return getId().equals(other.getId()); 
     } 
     return getActionList().equals(other.getActionList()) && getPosition().equals(other.getPosition()); 
    } 

    @Override 
    public int hashCode() { 

     if (StringUtils.isNotEmpty(getId())) { 
      return Objects.hash(this.getId()); 
     } else { 
      return Objects.hash(this.getActionList(), this.getPosition()); 
     } 
    } 
} 

我的問題是當我添加其中的一些刪除項目集合後,第一個元素也被刪除以及...

這裏是我一直在使用的測試:

@Test 
public void testAddAndDelete() { 

    final ActionList actionList = new ActionList(); 
    actionList.setActionId("testActionList"); 
    actionList.setName("testActionList"); 

    eManager.persist(actionList); 

    final String actionListId = actionList.getId(); 

    ActionList actionListFromDB = eManager.getById("ActionList", actionListId); 

    ActionItem itemToRemove = null; 

    for (int f = 0; f < 5; f++) { 

     final ActionItem newItem = new ActionItem(); 
     newItem.setActionList(actionListFromDB); 
     newStep.setName("itemname" + f); 
     actionListFromDB.getItems().add(newItem); 

     eManager.persist(newItem); 

     eManager.merge(actionListFromDB); 
     if (f == 3) { 
      itemToRemove = newItem; 
     } 
    } 

    eManager.commit(); 

    actionListFromDB = eManager.getById("ActionList", actionListId); 
    actionListFromDB.getItems().remove(itemToRemove); 

    eManager.merge(actionListFromDB); 
    eManager.commit(); 
} 

如果我刪除前右看看DB,我已經有了,在ActionItem表:

ID          ACTIONLIST_ID       POSITION NAME 
fbb3ddae-f8b7-11e4-8d6c-b9778e934988 f51846ca-f8b7-11e4-8d6c-1ba6d68e492d 0   itemname0 
fbb56453-f8b7-11e4-8d6c-dfaa3c092bd0 f51846ca-f8b7-11e4-8d6c-1ba6d68e492d 1   itemname1 
fbb60098-f8b7-11e4-8d6c-154d56d0c562 f51846ca-f8b7-11e4-8d6c-1ba6d68e492d 2   itemname2 
fbb675cd-f8b7-11e4-8d6c-21a3cc2b3c81 f51846ca-f8b7-11e4-8d6c-1ba6d68e492d 3   itemname3 
fbb71212-f8b7-11e4-8d6c-8f7c1ecfd151 f51846ca-f8b7-11e4-8d6c-1ba6d68e492d 4   itemname4 

然後我檢查的ActionList對象,其集合絕對有後的4個項目刪除(這是正確的),他們的位置屬性是失控(我期望,因爲我從中間刪除)。

但是,一旦我讓提交後刪除過關,表結束:

ID          ACTIONLIST_ID       POSITION NAME 
fbb56453-f8b7-11e4-8d6c-dfaa3c092bd0 f51846ca-f8b7-11e4-8d6c-1ba6d68e492d 1   itemname1 
fbb60098-f8b7-11e4-8d6c-154d56d0c562 f51846ca-f8b7-11e4-8d6c-1ba6d68e492d 2   itemname2 
fbb71212-f8b7-11e4-8d6c-8f7c1ecfd151 f51846ca-f8b7-11e4-8d6c-1ba6d68e492d 3   itemname4 

所以,正確的元素已經被刪除,但:

  1. 它也去掉了第一個
  2. 位置屬性不再正確(從1到3而不是從0開始)。

我知道現在支持的mappedBy的@OrderColumn,所以我不認爲這是 - 我懷疑貓膩與hashCode()方法,但事實證明,集合看起來不錯的的ActionList對象在被保留之前有點奇怪...

我在日誌中沒有從Hibernate中獲取任何異常。

回答

0

一如既往,一個映射問題。

上ActionItem改變映射到:

@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) 
    @JoinColumn(name = "actionList_id", nullable = false) 
    public ActionList getActionList() { 
    return this.actionList; 
    } 

固定它。

相關問題