-->
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.  [ 4 posts ] 
Author Message
 Post subject: session.setReadOnly() and TransientObjectException
PostPosted: Sat Jul 30, 2005 7:01 am 
Newbie

Joined: Thu Dec 16, 2004 7:50 am
Posts: 11
Hibernate version:
3.1beta1

Code:

Object rslt = session.load(SomeClass.class, itemId);
session.setReadOnly(rslt, true);

Stack trace of any exception that occurs:

org.hibernate.TransientObjectException: Instance was not associated with the session
org.hibernate.engine.StatefulPersistenceContext.setReadOnly(StatefulPersistenceContext.java:1134)
org.hibernate.impl.SessionImpl.setReadOnly(SessionImpl.java:1524)
.
.
.

Name and version of the database you are using:
MySQL 4.1.12a


I am trying to switch off the feature of Hibernate, which will save automatically the object (with its changed properties) retrieved by session.load() to DB when the session is flushed. I can not detach the object as I would like use the lazy collections feature, but I would like to make modifications to loaded object without saving it to DB (a sort of preview...)

This seemed to me the right way according to JavaDocs, but it looks like it is not...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 10:54 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Try session.evict()


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 3:56 pm 
Newbie

Joined: Thu Dec 16, 2004 7:50 am
Posts: 11
thank you very much, but this makes my lazy collections - which I like quite a lot - not working anymore, especially as I am using the Open Session in View approach with org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor

I was trying to find the problem with setReadOnly in the source code, but unfortunatelly I got lost too early...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 4:47 pm 
Newbie

Joined: Thu Dec 16, 2004 7:50 am
Posts: 11
after some modifications in the code I have found that if I add

@org.hibernate.annotations.Proxy(lazy=false)

to the class, which instance I am trying to make read-only, it works the right way. Looks almost like a bug.

anyway I bumped straight to another issue, as I would like to have in one session one instance with old values (as they are saved in the DB at that tome) and also another read-only copy of that instance, with applied modifications as a sort of preview-new-modifications-and-compare-with-previous-state matter.

so basically I am doing this

Object new = session.load();
session.setReadOnly(new, true);
new = makeTheModifications(new);
Object old = session.load();

but unfortunatelly this is not working, probably due to some cache reasons so even if I have the following code snippet

getSessionFactory().evict(SomeClass.class, itemId);
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
session.setCacheMode(CacheMode.IGNORE);
Object rslt = session.load(SomeClass.class, itemId);
getSessionFactory().evict(SomeClass.class, itemId);
rslt = session.load(SomeClass.class, itemId);

the second load() call will not force the item to reload from DB = second SQL statement. Therefore I am not able to get the original state again from DB.

I can not use session.reload() as this will refresh both the copies of the object.

I can not use session.evict() as mentioned earlier.

I can not make directly some sort of copy of the object before applying modifications, because the first load() and the second load() actually happens on completely different places. If you know Spring Web MVC, then first is in formBackingObject method to create the command object (for applying modifications) and the second one is in onSubmit method to create the object with current (old) values from DB. Applying of the modifications to the command object is located (as usually in Spring Web MVC) in the onBind method which is called before onSubmit and after formBackingObject. So if I would be able to make copy in the formBackingObject, I have to solve how to get it into the onSubmit method in some nice and clear way...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.