-->
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.  [ 9 posts ] 
Author Message
 Post subject: NonUniqueObjectException after evict?
PostPosted: Wed Jul 12, 2006 10:59 am 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
Hi,

Here's what I'm doing:

Long id=401;

CarInfo carInfo=session.load(..., id);

session.evict(carInfo);

carInfo= session.load(..., id);

session.update(carInfo);

session.flush();// throws NonUniqueObjectException for CarInfo#401


Am I doing something wrong? I did evict the object from session - why is Hibernate complaining about it still being there?

Any help on the subject would be greatly appreciated.
Thanks,

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
you do evict it... and then immediately reload it ;)

are you calling session.update(carInfo) because you want to update the row in the database? That is not the purpose of the update() method...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 12:10 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
Yes, I'm calling Session.update to persist changes to an existing record.
I took a look at the API doc in the meantime, but to be honest I don't understand anymore what the update method does. The catch is that I've been using update after load all the time to make changes to existing rows, and I never got this exception until using that evict example.

Still, I'm not interested in why load/update works without evict, and does not work with it - I just want to make it work. So can you tell me which method should I use to persist changes made on existing db row (if update isn't the right one)? Does one of those Session methods (save, saveOrUpdate) guarantee to throw an exception if object does not exist? The way I built my DAO layer I have separate methods for inserts and updates, so I'd like to guarantee that they will work just like that.

Thank you for your help!

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 12:55 pm 
Beginner
Beginner

Joined: Fri Feb 17, 2006 1:28 pm
Posts: 24
I would say the reason you are getting NonUniqueObjectException is because the object didnt get evited from the Session.
After you evict the object call session.contains(object) and see if that returns true. If it does, then the object is still in session and has not been evicted.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 1:57 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
I checked (called Session.contains), but it looked like the object was evicted after all. So something else is a problem. Is anyone else using Session.update to update db rows? Is that method meant for something else?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 2:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You *do* evict the entity; but you then immediately turn around and load the same entity. The process of evicting removes the entity from what is termed the persistence-context. The process of loading adds the entity to the persistence context.

update() is meant to take a detached object and reassociate it with the persistence-context.

If any of these terms are unfamiliar you really need to go through the documentation...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 2:55 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
My point is that this:

carInfo= session.load(..., id);
session.update(carInfo);
session.flush();

Works just fine. So I have the object in persistence context, I'm calling update on it, and all is well. What I don't understand is why this:

carInfo= session.load(..., id);
session.evict(carInfo);
carInfo=session.load(...,id);
session.update(carInfo);
session.flush();

Does not work? If evict removes object from persistence context, and load puts it right back there, then what's the problem? evict and load should even out each other, and I should be back to my initial scenario, with object stored in persistence context and ready for update.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 9:14 am 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
<bump> Any takers on this one? I'd appreciate any help ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 11:33 am 
Beginner
Beginner

Joined: Fri Feb 17, 2006 1:28 pm
Posts: 24
Well can I ask, did you change any config to allow you to evict an object from the Session?

I tried to do it there a few days ago but it didn’t work. If anyone knows about evicting an object from a session, please share!!!

I have tried to load the object and then evict it. Then when i check to see if the object has been evicted, I find that the object is still in the Session and any changes to it will be persisted!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.