Answer the question
In order to leave comments, you need to log in
Hibernate, OneToMany Cascading adding/removing children
There is a User class:
@Entity
@Table(name = "users")
public class User implements Serializable, Annotation {
@Id
@GeneratedValue(generator = "system-uuid", strategy = GenerationType.IDENTITY)
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Column(name = "uuid", unique = true)
protected String uuid;
@Column(name = "username")
protected String username;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user", cascade = CascadeType.ALL)
private Set<UserItem> userItems;
}
@Entity
@Table(name = "user_item")
@AssociationOverrides({
@AssociationOverride(name = "pk.user",
joinColumns = @JoinColumn(name = "user_uuid")),
@AssociationOverride(name = "pk.ItemShop",
joinColumns = @JoinColumn(name = "item_shop_uuid")) })
public class UserItem implements Serializable {
@EmbeddedId
protected UserItemId pk = new UserItemId();
@Transient
public User getUser() {
return getPk().getUser();
}
public void setUser(User user) {
getPk().setUser(User);
}
@Transient
public ItemShop getItemShop() {
return getPk().getItemShop();
}
public void setItemShop(ItemShop ItemShop) {
getPk().setItemShop(ItemShop);
}
}
UserItem userItem = new UserItem();
userItem.setUser(user);
userItem.setItemShop(ItemShop);
user.getUserItems().add(userItem);
session.saveOrUpdate(user);
session.flush();
Set<UserItem> userItems = user.getUserItems();
UserItem userItem = userDAO.findUserItem(user, itemShop);
userItems.remove(userItem);
session.saveOrUpdate(user);
session.flush();
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.test.User#2d0767d8-effb-41f2-8910-5729a460527d]
User user = UserDetails.getUser();
Set<UserItem> userItems = user.getUserItems();
UserItem userItem = UserDAO.findUserItem(user, itemShop);
UserItems.remove(UserItem);
session.merge(user);
session.flush();
session.clear();
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.test.User#2d0767d8-effb-41f2-8910-5729a460527d]
Answer the question
In order to leave comments, you need to log in
It seems that the error occurs due to unnecessary manipulations with entities. Look at the contents of the session under debugging, it collects a list of all objects to be saved when flush / commit is called. Most likely, in addition to the user entity that is clearly visible in the code, at some point (perhaps userDAO.findUserItem ?) another instance of User appears in the session, for example, in UserItemId. You need to ensure that all relationships refer to the same object instance. To do this, try to perform all operations in one session, i.e., if one session is already open in the code execution thread, do not open other sessions (in userDAO.findUserItem ?) until the current session is closed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question