2

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.

Suemayah Eldursi
  • 969
  • 4
  • 14
  • 36
  • You might add more details and code if you want use to help you, because at this point need more. – Zorglube Apr 25 '17 at 15:37
  • You can try the accepted answer here: http://stackoverflow.com/questions/3609653/hibernate-error-org-hibernate-nonuniqueobjectexception-a-different-object-with?answertab=active#tab-top – Hasan K Apr 25 '17 at 16:07

0 Answers0