-->
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.  [ 7 posts ] 
Author Message
 Post subject: save & delete in one session throws NonUniqueObjectExcep
PostPosted: Wed May 17, 2006 1:49 pm 
Newbie

Joined: Tue Mar 28, 2006 11:46 am
Posts: 10
Hi,

I am using Hibernate version: 3.13

I want to execute multiple operations in one session for my User object. Basically first I am creating a user object and then deleting that object in same session. But deletion of object fails with following exception:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

I have tried using merge() and evict() before deleting the object but it fails.

Here is my program:

DAO CLASS METHODS
=====================
public User create(User user) {
String id = (String)session.save(user);
User retUser = (User)user.clone();
retUser.setId(id);
return retUser;
}

public void delete(String id) {
User user = new User();
user.setId(id);
session.delete(user);

}

public startOperation() {
session = HibernateFactory.getSession();
tx = session.beginTransaction();
}

public void commit() {
tx.commit();
}

public void rollback() {
tx.rollback();
}

public void close() {
session.close();
}

TEST CLIENT
===================
try {
dao.startOperation();
// add an object
User user = new User();
User retUser = dao.create(user);
// delete object
dao.delete(retUser.getId());
dao.commit();
} catch (Exception ex) {
dao.rollback();
} finally {
dao.close();
}


Thanks.

Sanjeev


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 2:40 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
try {
   dao.startOperation();
   // add an object
   User user = new User();
   User retUser = dao.create(user);

   //FLUSH THE SESSION HERE

   // delete object
   dao.delete(retUser.getId());
   dao.commit();
} catch (Exception ex) {
   dao.rollback();
} finally {
   dao.close();
}


flush the session as indicated by the comment line in the code.

this topic (http://forum.hibernate.org/viewtopic.php?t=959555) already discussed and have some comments


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 2:44 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
This is the way Hibernate works.
You can:
- evict object from session after saving; (not avisable)
- or you can check for the object presence in the sesion in the delete method:
O o = session.get( User.class, id );
if( o != null ){
session.delete( o );
}else{
your current code in the delete method
}

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 3:29 pm 
Newbie

Joined: Tue Mar 28, 2006 11:46 am
Posts: 10
Thanks.

I have tried to get the object from session and it works.

Does this mean if I do more operations in one session such as update or retreive then I should try to get the object first from session.

Is there will be any performance issue when I will first try to get the object from session (like any database call).


DAO CLASS METHODS
=====================
public void delete(String id) {
Object o = session.get( User.class, id );
if( o != null ){
session.delete( o );
}else{
User user = new User();
user.setId(id);
session.delete(user);
}


public User retrieve(String id) {
Object obj = session.get(User.class, id);
if (obj == null) {
session.load(User.class, id);
}
}



TEST CLIENT
===================
try {
startOperation();
// add an object
User retUser = dao.create(user);

// get object
User retuser2 = dao.retrieve(retUser.getId());


// delete object
dao.delete(retUser.getId());

dao.commit();
} catch (Exception ex) {
dao.rollback();
} finally {
dao.close();
}

Thanks.

Sanjeev


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 3:42 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
session.lock( userObj, LockMode.NONE );

will not require a SELECT.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 4:12 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
ssingal wrote:
Thanks.
Is there will be any performance issue when I will first try to get the object from session (like any database call).
Sanjeev


No.
session.load( ) tries to loads object from DB, get() - does not

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 5:09 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello,
you can do multiple actions on objects in the session without flushing. Flushing is needed in rare cases for objects in relation when you need to create a db generated primary key first.

Your problem is IMHO that you are cloning the user. You assign the primary key, save the user then clone it and start deleting the cloned one. Than Hibernate detects that there is allready a user with the same primary key in the session. Do not clone.

Best Regards
Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


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

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.