-->
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.  [ 7 posts ] 
Author Message
 Post subject: Setting the same value->marked for update?
PostPosted: Fri Oct 21, 2005 5:55 am 
Newbie

Joined: Tue Jun 15, 2004 9:46 am
Posts: 3
Location: Kista, Sweden
Hi,

I didn't find an answer to this in the docs, so here goes:

Does setting a field in an object mark it for updating if I set it to the same value as before? Or does Hibernate check that?


Top
 Profile  
 
 Post subject: Re: Setting the same value->marked for update?
PostPosted: Fri Oct 21, 2005 7:58 am 
Newbie

Joined: Wed Oct 05, 2005 1:20 pm
Posts: 15
Location: Russia Izhevsk
NiklasH wrote:
Does setting a field in an object mark it for updating if I set it to the same value as before? Or does Hibernate check that?


Hierbnate have a strategy for detecting which persistent objects have been modified by the application. It is called automatic dirty checking (an object with modifications that haven’t yet been propagated to the database is considered dirty). Hibernate can detect exactly which attributes have been modified, so it’s possible to include only the columns that need updating in the SQL UPDATE statement. By default, Hibernate includes all columns in the SQL UPDATE statement (hence, Hibernate can generate this basic SQL at startup, not at runtime). If you only want to update modified columns, you can enable dynamic SQL generation by setting dynamic-update="true" in a class mapping.

--
Andrey Grebnev
http://www.jroller.com/page/agrebnev


Top
 Profile  
 
 Post subject: Re: Setting the same value->marked for update?
PostPosted: Fri Oct 21, 2005 9:11 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
NiklasH wrote:
Hi,

I didn't find an answer to this in the docs, so here goes:

Does setting a field in an object mark it for updating if I set it to the same value as before? Or does Hibernate check that?


Hibernate will NOT issue an update if the values haven't changed.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Setting the same value->marked for update?
PostPosted: Fri Oct 21, 2005 2:06 pm 
Newbie

Joined: Tue Jun 15, 2004 9:46 am
Posts: 3
Location: Kista, Sweden
pksiv wrote:
NiklasH wrote:
Hi,

I didn't find an answer to this in the docs, so here goes:

Does setting a field in an object mark it for updating if I set it to the same value as before? Or does Hibernate check that?


Hibernate will NOT issue an update if the values haven't changed.


Thanks, that's what I wanted to hear :)


Top
 Profile  
 
 Post subject: Re: Setting the same value->marked for update?
PostPosted: Fri Oct 21, 2005 2:14 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
BTW: While I was pretty sure that was the correct answer, before I submitted it, I took 5 minutes and wrote a TestCase and gave it a try. I find that having a test project in my IDE is invaluable for determing the actual behavior of Hibernate. Or any other software.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Setting the same value->marked for update?
PostPosted: Fri Oct 21, 2005 11:49 pm 
Newbie

Joined: Wed Oct 05, 2005 1:20 pm
Posts: 15
Location: Russia Izhevsk
pksiv wrote:
Hibernate will NOT issue an update if the values haven't changed.


You are wrong if I understand the initial question correctly. We say about "a field in an object", in other words about updating one separate field of the object which is mapped to some field(s) of database table(s).

Of course first of all we call

Code:
   public boolean isDirty(Object old, Object current, SessionImplementor session)


for each field of obejct in order to understand we need to update the obejct in a whole or not. Then if some field (inlucding collections, components) was changed we perform the following method:

Code:
   /**
    * Update an object
    */
   public void update(final Serializable id,
                  final Object[] fields,
                  final int[] dirtyFields,
                  final boolean hasDirtyCollection,
                  final Object[] oldFields,
                  final Object oldVersion,
                  final Object object,
                  final Object rowId,
                  final SessionImplementor session)
         throws HibernateException {

      //note: dirtyFields==null means we had no snapshot, and we couldn't get one using select-before-update
      //     oldFields==null just means we had no snapshot to begin with (we might have used select-before-update to get the dirtyFields)

      final boolean[] tableUpdateNeeded = getTableUpdateNeeded( dirtyFields, hasDirtyCollection );
      final int span = getTableSpan();

      final boolean[] propsToUpdate;
      final String[] updateStrings;
      if ( entityMetamodel.isDynamicUpdate() && dirtyFields != null ) {
         // For the case of dynamic-update="true", we need to generate the UPDATE SQL
         propsToUpdate = getPropertiesToUpdate( dirtyFields, hasDirtyCollection );
         // don't need to check laziness (dirty checking algorithm handles that)
         updateStrings = new String[span];
         for ( int j = 0; j < span; j++ ) {
            updateStrings[j] = tableUpdateNeeded[j] ?
                  generateUpdateString( propsToUpdate, j, oldFields, j == 0 && rowId != null ) :
                  null;
         }
      }
      else {
         // For the case of dynamic-update="false", or no snapshot, we use the static SQL
         updateStrings = getUpdateStrings(
               rowId != null,
               hasUninitializedLazyProperties( object, session.getEntityMode() )
            );
         propsToUpdate = getPropertyUpdateability( object, session.getEntityMode() );
      }

      for ( int j = 0; j < span; j++ ) {
         // Now update only the tables with dirty properties (and the table with the version number)
         if ( tableUpdateNeeded[j] ) {
            updateOrInsert(
                  id,
                  fields,
                  oldFields,
                  j == 0 ? rowId : null,
                  propsToUpdate,
                  j,
                  oldVersion,
                  object,
                  updateStrings[j],
                  session
               );
         }
      }
   }



Here the decision is taken about which fields (of which tables) should be included into updating SQL query string.

In case of dynamic-update="true" if the obejct's field is NOT marked as dirty ("mark it for updating" - citation of initial question) the database table(s) field(s) will NOT be included into updating SQL.

_________________
--
Andrey Grebnev
http://www.jroller.com/page/agrebnev


Top
 Profile  
 
 Post subject: Re: Setting the same value->marked for update?
PostPosted: Fri Oct 21, 2005 11:49 pm 
Newbie

Joined: Wed Oct 05, 2005 1:20 pm
Posts: 15
Location: Russia Izhevsk
pksiv wrote:
Hibernate will NOT issue an update if the values haven't changed.


You are wrong if I understand the initial question correctly. We say about "a field in an object", in other words about updating one separate field of the object which is mapped to some field(s) of database table(s).

Of course first of all we call

Code:
   public boolean isDirty(Object old, Object current, SessionImplementor session)


for each field of obejct in order to understand we need to update the obejct in a whole or not. Then if some field (inlucding collections, components) was changed we perform the following method:

Code:
   /**
    * Update an object
    */
   public void update(final Serializable id,
                  final Object[] fields,
                  final int[] dirtyFields,
                  final boolean hasDirtyCollection,
                  final Object[] oldFields,
                  final Object oldVersion,
                  final Object object,
                  final Object rowId,
                  final SessionImplementor session)
         throws HibernateException {

      //note: dirtyFields==null means we had no snapshot, and we couldn't get one using select-before-update
      //     oldFields==null just means we had no snapshot to begin with (we might have used select-before-update to get the dirtyFields)

      final boolean[] tableUpdateNeeded = getTableUpdateNeeded( dirtyFields, hasDirtyCollection );
      final int span = getTableSpan();

      final boolean[] propsToUpdate;
      final String[] updateStrings;
      if ( entityMetamodel.isDynamicUpdate() && dirtyFields != null ) {
         // For the case of dynamic-update="true", we need to generate the UPDATE SQL
         propsToUpdate = getPropertiesToUpdate( dirtyFields, hasDirtyCollection );
         // don't need to check laziness (dirty checking algorithm handles that)
         updateStrings = new String[span];
         for ( int j = 0; j < span; j++ ) {
            updateStrings[j] = tableUpdateNeeded[j] ?
                  generateUpdateString( propsToUpdate, j, oldFields, j == 0 && rowId != null ) :
                  null;
         }
      }
      else {
         // For the case of dynamic-update="false", or no snapshot, we use the static SQL
         updateStrings = getUpdateStrings(
               rowId != null,
               hasUninitializedLazyProperties( object, session.getEntityMode() )
            );
         propsToUpdate = getPropertyUpdateability( object, session.getEntityMode() );
      }

      for ( int j = 0; j < span; j++ ) {
         // Now update only the tables with dirty properties (and the table with the version number)
         if ( tableUpdateNeeded[j] ) {
            updateOrInsert(
                  id,
                  fields,
                  oldFields,
                  j == 0 ? rowId : null,
                  propsToUpdate,
                  j,
                  oldVersion,
                  object,
                  updateStrings[j],
                  session
               );
         }
      }
   }



Here the decision is taken about which fields (of which tables) should be included into updating SQL query string.

In case of dynamic-update="true" if the obejct's field is NOT marked as dirty ("mark it for updating" - citation of initial question) the database table(s) field(s) will NOT be included into updating SQL.

_________________
--
Andrey Grebnev
http://www.jroller.com/page/agrebnev


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