-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to reparent
PostPosted: Wed Oct 17, 2012 1:47 pm 
Newbie

Joined: Tue Jul 19, 2005 1:28 pm
Posts: 17
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.