-->
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: NonUniqueObjectException when adding multiple children
PostPosted: Fri Feb 09, 2007 10:38 am 
Newbie

Joined: Fri Feb 09, 2007 10:29 am
Posts: 1
I have a one-to-many entity defined using Hibernate Tools in Eclipse.

I am trying to add many children to a parent and invoke the update method on the session and it throws NonUniqueObjectException. I spent nearly 3 days researching and no luck. Any help would be highly appreciated.

I have a table called Role and another table called MenuRole. MenuRole contains many rows for each role.

Here is my code

public void saveRole(RoleVOType roleVO) throws Exception {

log.debug("Invoking the Save Role Web Service Method");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Calendar rightnow = Calendar.getInstance();

try {

Role role = getRoleManagerService().getRole(roleVO.getRoleID());
role.setRoleDesc(roleVO.getDescription());
role.setDtUpdated(rightnow.getTime());
role.setIdUpdated(roleVO.getIdUpdated());
java.util.Collection children = role.getMtcseMenuRoles();

// I now remove all menu roles from the parent and it works
Iterator it = children.iterator();
while (it.hasNext()) {
MenuRole m = (MenuRole)it.next();
log.debug("Deleting Menu Role for " + m.getMtcseMenu().getName() + " for Role " + m.getMtcseRole().getRoleId());
it.remove();
getRoleManagerService().deleteMenuRole(m.getMenuRoleSeq());
}

// Now adding new menuRole entities to the parent role
for (int i = 1; i < 5; i++) {
Menu menu = getMenuManagerService().getMenu(i);
MenuRole menuRole = new MenuRole();
menuRole.setDtCreated(rightnow.getTime());
menuRole.setDtUpdated(rightnow.getTime());
menuRole.setIdCreated("SYSTEM");
menuRole.setIdUpdated("SYSTEM");
menuRole.setMtcseRole(role);
menuRole.setMtcseMenu(menu);
log.debug("Adding Menu Role for " + menuRole.getMtcseMenu().getName() + " for Role " + menuRole.getMtcseRole().getRoleId());
role.getMtcseMenuRoles().add(menuRole);
}

// Finally calling the update method on hibernate session and it fails
getRoleManagerService().saveRole(role);
} catch (Exception e) {
log.debug(e.getMessage());
e.printStackTrace();
throw e;
}
return getAllRoles();
}

I use Annotations and have the mappedBy attribute set correctly and also use lazy load.

Thanks in advance

RK


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.