相關聯的I具有以下面的方式與兩個彼此對象:休眠更新例外:一個不同的對象使用相同的標識符值已經與會話
Item.java
@Entity
@Table(name = "item")
public class Item {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "item", cascade=CascadeType.ALL)
private Set<ItemConfiguration> ItemSettings = new HashSet<ItemSettings>();
}
ItemSetting。 Java的
@Entity
@Table(name = "item_setting")
public class ItemSetting {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "type")
private String type;
@Column(name = "value")
private String value;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id", nullable = false)
private Item item;
}
我試圖更新由以下語句的順序設置:
ItemSettings settings = item.getSettingByType(type); // returns one of the settings that has matching type
settings.setValue("new value");
this.itemDAO.updateItem(item);
這將導致一個例外:
不同的對象有同樣的標識值已經 與會話
我不明白爲什麼會發生相關。我不是多次從數據庫加載相同的setting
。方法item.getSettingByType(type)
通過遍歷與項目關聯的一組設置來獲取setting
。
如果您想用於幫助您,可能會添加更多詳細信息和代碼,因爲此時需要更多。 – Zorglube
您可以嘗試在這裏接受的答案:http://stackoverflow.com/questions/3609653/hibernate-error-org-hibernate-nonuniqueobjectexception-a-different-object-with?answertab=active#tab-top –