Hi, I am having trouble reparenting a child object.
I am using Hibernate 4.1
Here are my entities
Code:
class Users {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "users", cascade=CascadeType.ALL, orphanRemoval=true)
@BatchSize(size=20)
public Set<UserPermissions> getUserPermissionses() {...}
}
class UserPermissions {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "permissionsId", column = @Column(name = "permissions_id", nullable = false)),
@AttributeOverride(name = "userId", column = @Column(name = "user_id")) })
public UserPermissionsId getId() {....}
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "permissions_id", nullable = false, insertable = false, updatable = false)
public Permissions getPermissions() {....}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", insertable = false, updatable = false)
public Users getUsers() {....}
}
@Embeddable
public class UserPermissionsId {
@Column(name = "permissions_id", nullable = false)
public int getPermissionsId() { .... }
@Column(name = "user_id")
public int getUserId() {....}
}
When I try to perform the reparenting like this
Code:
UserPermission perm = UserPermissionDAO.findByExample(example);
Users newUser = UserDAO.findById(23);
perm.getUser().getUserPermissionses().remove(perm);
perm.setUser(newUser);
newUser.getUserPermissionses().add(perm);
I am getting exception
Code:
org.springframework.dao.InvalidDataAccessApiUsageException: deleted object would be re-saved by cascade (remove deleted object from associations): [UserPermissions#UserPermissionsId@d4cf]
I even tried setting
Code:
perm.getId().setUserId(newUser.getUserId());//which throws a jdbc batch size exception
perm.getId().setUserId(0);//throws the same deleted object would be re-saved exception
What is the proper way to perform the reparenting. Are my mappings in any way incorrect?
Thanks