-->
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: Trying to understand setReadOnly(obj,false)...
PostPosted: Thu Jul 16, 2009 12:16 am 
Newbie

Joined: Wed Mar 19, 2008 10:46 pm
Posts: 5
Hi,


I'm trying to understand the purpose of setReadOnly() method of hibernate's session class.

I have the following (pseudo)
Code:
Cat cat=session.get(Cat.class,1L);
session.setReadonly(cat, true);
cat.setName("changed");
....
...
...
session.setReadonly(cat, false);
session.saveOrUpdate(cat);


Apparently, with this code, the name of the cat is not going to get updated to 'changed', even
though I've flagged the setReadonly as 'false'.
Also... even if I remove the call to session.setReadonly(cat, true), the cat is never going to get updated.
I've done a bit of reading... and apparently by setting setReadOnly(..,false), the 'loaded' state is going to
get synced with the current state, hence Hibernate's dirty checking always return false (which means
object changes is not going to get persisted?).... if so.... what is the purpose of giving the boolean parameter
to setReadOnly method?


Am I missing something here? Maybe the method name is misleading? Apology if the pseudo code
above is not clear... but I hope you get the idea..





Regards,


Alex.


Top
 Profile  
 
 Post subject: Re: Trying to understand setReadOnly(obj,false)...
PostPosted: Thu Jul 16, 2009 4:26 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
When you set the read only flag as true
Code:
session.setReadonly(cat, true);


it tells the hibernate not to maintain the snapshot of this object. Changing any property of persistance object will not result in update. As the snapshot of object is not there for dirty checking.

Code:
session.setReadonly(cat, false);


This will indicate hibernate to maintain snapshot for cat object. This code will work as expected if changes are made after setting the readOnly flag false. In that case hibernate
will compare it to old snapshot of cat object and update database.

Code:
Cat cat=session.get(Cat.class,1L);
session.setReadonly(cat, true);
session.setReadonly(cat, false);
cat.setName("changed");
....
...
...

session.saveOrUpdate(cat);


Top
 Profile  
 
 Post subject: Re: Trying to understand setReadOnly(obj,false)...
PostPosted: Thu Jul 16, 2009 7:50 am 
Newbie

Joined: Wed Mar 19, 2008 10:46 pm
Posts: 5
Hi,


Thanks for the reply. I've just written a simple test myself, and confirmed the behaviour as you described.
Here is what I observed:
Code:
Cat cat = getCurrentSession().load(Cat.class, 1L);  // cat with name 'bill'
getCurrentSession().setReadonly(cat,true);
getCurrentSession().setReadonly(cat,false);
cat.setName('frank');
..
..

after the call above, cat will have the name 'frank'..

but... if I do the following:
Code:
Cat cat = getCurrentSession().load(Cat.class, 1L);  // cat with name 'bill'
getCurrentSession().setReadonly(cat,true);
cat.setName('frank');
getCurrentSession().setReadonly(cat,false);

..
..


That cat will still have the name 'bill'. In my case, I need to call the 'setReadonly(true)' before the changes made to the object, and I want to call 'setReadOnly(false)' at some point later (after the changes get applied to the object), and get the changes get persisted..... can anyone suggest a solution?



Thanks in advance!



Regards,


Alex.


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.