-->
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.  [ 3 posts ] 
Author Message
 Post subject: "session.load" followed immediately by "sessi
PostPosted: Mon Apr 17, 2006 10:23 pm 
Newbie

Joined: Mon Apr 17, 2006 10:09 pm
Posts: 1
Hi,

I am using Hibernate3.0 (managed by Spring 1.2).

I created a persistent instance of a class by loading it from the session (the record corresponding to the Identity value exists in the DB Table).

Immediately following the load, i did "session.save(myObject)" and then "session.flush()".

Here is the sample code:
MyObject myObject = (MyObject)session.load(MyObject.class, myObjectId);
session.save(myObject);
session.flush();

This resulted in a "PersistentObjectException" with the message "Uninitialized proxy passed to save()".

I tried this purely out of curiosity, but I haven't been able to understand the outcome of this trial. Can anyone please describe this behaviour?

Regards,
Anil


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 5:35 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
You shouldn't use save method, you should use update method (or saveOrUpdate method)


Top
 Profile  
 
 Post subject: Re: "session.load" followed immediately by "s
PostPosted: Wed Apr 19, 2006 8:34 pm 
Beginner
Beginner

Joined: Mon Nov 29, 2004 2:26 pm
Posts: 28
manil wrote:
MyObject myObject = (MyObject)session.load(MyObject.class, myObjectId);


1. Since class-level lazy loading is enabled for MyObject, Hibernate is giving you a proxy for the object with myObjectId (it's not even hitting the database).

manil wrote:
session.save(myObject);


Now you are passing an object that is already persistent to Session.save(), which doesn't make sense. You only need to save() it the first time. Then when you load() it Hibernate keeps tracks of what's changed and flushed it back to the database. The exception says "Uninitialized proxy" because of what I explained in (1).

To fix your code, just remove the call to session.save() and do myObject.setSomething(). You should probably read the section about Hibernate object lifecycle in the Hibernate in Action book.


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