I have two object related to each other in the following way:
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;
}
I am trying to update the settings for an order by the following statement:
ItemSettings settings = item.getSettingByType(type); // returns one of the settings that has matching type
settings.setValue("new value");
this.itemDAO.updateItem(item);
This results in an exception:
a different object with the same identifier value was already associated with the session
I don't understand why this is happening. I am not loading the same setting
from the database more than once. The method item.getSettingByType(type)
gets the setting
by iterating through the set of settings associated with the item.