-->
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.  [ 6 posts ] 
Author Message
 Post subject: updating serialized / unserialized object
PostPosted: Fri Aug 06, 2004 1:24 pm 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:17 pm
Posts: 44
I have an object that was loaded, serialized and unserialized. When I try to update it (using the same session that loaded it), I get this error:

Quote:

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: com.agentscape.associate.to.ServerInfoTO
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1673)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1442)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1469)
at net.sf.hibernate.impl.SessionImpl.update(SessionImpl.java:1354)
at com.agentscape.associate.bo.BaseBO.update(BaseBO.java:45)
at com.agentscape.associate.test.SerializableTest.doLoad(SerializableTest.java:61)
at com.agentscape.associate.test.SerializableTest.<init>(SerializableTest.java:21)
at com.agentscape.associate.test.SerializableTest.main(SerializableTest.java:69)


Is it a "bad thing" to update an object that was serialized / unserialized? If I comment out the serialize / unserialize part, the update works great.

I can post an example if needed.

I'm using Hibernate 2.1.4 with MySQL 4.0.20a.

Thanks in advance,
Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2004 2:42 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
The 'problem' is that the SessionImpl uses instance equality to check if the object is already in the session. Since you've serialized/deserialized your objects then you have new instances of them.

You are then turning around (using the same Session) and attempting to update the object. It doesn't find it in the session (due to different instances) but it DOES find another object (your original) with the same identifier. The proverbial all hell breaks loose at that point.

Do you need to have the same session? Do you need to serialize?

I feel your pain.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2004 2:44 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Uhm, guys, this is easier than you make it. Also check "saveOrUpdateCopy()", which is the counterpart to "saveOrUpdate()". In real applications with long running transactions you need both. And to understand long running transactions and how they are implemented, you need Hibernate in Action :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2004 2:51 pm 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:17 pm
Posts: 44
christian wrote:
Uhm, guys, this is easier than you make it. Also check "saveOrUpdateCopy()", which is the counterpart to "saveOrUpdate()". In real applications with long running transactions you need both. And to understand long running transactions and how they are implemented, you need Hibernate in Action :)


That's exactly what I was looking for.

You're right, I think it is time that I picked myself up a copy.

Thanks for the help.


Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2004 2:52 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
You know Christian, you're right. I need to go and read that right now while it's in my head. I'm trying to get my head wrapped around some of the finer points of all of this. (including Lazy loading objects that have dirty components ;)

I'll be formulating my code hack a bit later :)


Top
 Profile  
 
 Post subject: Slightly different problem
PostPosted: Tue Aug 10, 2004 7:24 am 
Newbie

Joined: Tue Aug 10, 2004 7:17 am
Posts: 1
Location: Sevastopol, Crimea, Ukraine
Hi guys,

I have slightly different, but yet similar problem. Maybe you could help me.

The problem is that I have the following exception each time I try to load or delete item using Hibernate from database:

de.nava.informa.core.ChannelBuilderException: Unable to reload item: a different object with the same identifier value was already associated with the session: 3, of class: de.nava.informa.impl.hibernate.Item
at de.nava.informa.impl.hibernate.ChannelBuilder.reload(Unknown Source)
at de.nava.informa.utils.PersistChanGrpMgr.deleteItemFromChannel(Unknown Source)
at com.salas.bb.informa.InformaBackEnd.deleteArticleFromCGE(InformaBackEnd.java:388)

---

ChannelBuilder.reload() by now simply uses Hibernate Session load(...) method to load contents of item back from database before operations. Even iif comment line with reload() method call in PersisteChanGrpMgr.deleteItemFromChannel() the next operation will fail as well (which is Session.delete()) with the same error.

Any tips?

_________________
Thanks,

Aleks


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