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.  [ 2 posts ] 
Author Message
 Post subject: setReadOnly(false) resets state
PostPosted: Mon Nov 19, 2007 9:26 am 
Newbie

Joined: Mon Nov 19, 2007 8:40 am
Posts: 5
Using Session.setReadOnly(false) seems to reset the state of an entity even if it was already in the session read/write.

In order to try to minimise dirty checking time during flushes, we experimented with a method that essentially did this:

Code:
Entity getEntity(Class clazz, int id, boolean readOnly)
{
   // (Code to get session (via Spring HibernateTemplate) omitted)

   session.get(clazz, id);
   session.setReadOnly(readOnly);
}


We found that if we did this:

Code:
Entity entity = getEntity(clazz, id, false);
entity.setSomeProperty(someValue);
session.save(entity);


And then in some other part of the code (as part of the same transaction) this (for the same entity):

Code:
Entity entity = getEntity(clazz, id, false);
entity.setSomeProperty(someOtherValue);
session.save(entity);


The entity's property was not updated to someOtherValue.

Looking at org.hibernate.engine.EntityEntry.setReadOnly() it seams that the loaded state is reverted to that in the persister regardless of whether the state is currently already read/write.

Is this the correct behaviour? I could understand that going from read-only to read/write would overwrite any updates done while read-only.

It would be nice if there were some way to tell whether an entity was already in the session read/write. Then we could write a general purpose method to get an entity and set it read-only only if it's not already in the session read/write.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 4:18 pm 
Newbie

Joined: Wed Jan 28, 2009 4:04 pm
Posts: 1
Problem
=======
setReadOnly(false) API disables dirty-checking when component mapping is utilized.

Environment
==========
hibernate-3.2.6.ga

Background
===========

1. Session holds a pair of "current managed object -> loaded object".
This pair is used for tracking changes.

Hibernate maintaind deep copy of the object after load, untill....

2. Session.setReadOnly api synches loaded state with the current one using shallow

copy - if object is NOT read only.

Here is excerpt fron EntityEntry:

Code:
   public void setReadOnly(boolean readOnly, Object entity) {
      if (status!=Status.MANAGED && status!=Status.READ_ONLY) {
         throw new HibernateException("instance was not in a valid

state");
      }
      if (readOnly) {
         setStatus(Status.READ_ONLY);
         loadedState = null;
      }
      else {
         setStatus(Status.MANAGED);
         loadedState = getPersister().getPropertyValues(entity,

entityMode); // it's here!!!, see step (3) below
      }
   }

Scenario
===============

(1) load object
(2) set read only to false ==> loaded state = current state (component's REFERENCES -

to the same component - got copied)
(3) update component's element - updated in both current and loaded state
(4) dirty check yields in "no changes"
Code:


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