title: Is Hibernate 4.3 @DynamicUpdate really does not include null values?
body: <p>I have been trying to implement entity updating using <strong>@DynamicUpdate</strong>. Documentation (<a href="http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/" rel="nofollow">http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/</a>) says:  </p>
  
  <blockquote>
    <p>dynamicInsert / dynamicUpdate (defaults to false): specifies that INSERT / UPDATE SQL should be generated at runtime and <strong>contain only the columns whose values are not null</strong>.</p>
  </blockquote>
  
  <p>But I didn't manage to make it works, so I dug into the sources of AbstractEntityPersister, and saw this:</p>
  
  <pre><code>generateUpdateString( propsToUpdate, j, oldFields, j == 0 &amp;&amp; rowId != null )
  </code></pre>
  
  <p>It is a method for generating SQL query string, based on 'propsToUpdate' which is boolean array obtained from:</p>
  
  <pre><code>getPropertiesToUpdate( dirtyFields, hasDirtyCollection );
  </code></pre>
  
  <p>where 'dirtyFields' is an integer array of id's of 'dirty' columns.</p>
  
  <p>But nowhere in </p>
  
  <pre><code>getPropertiesToUpdate(final int[] dirtyProperties, final boolean hasDirtyCollection)
  </code></pre>
  
  <p>i was able to find mechanism for checking if modified column is not null, so even when entity i want to merge has some null fields, all of them are updated in the DB, overriding existing data with nulls.</p>
  
  <p>My question is: where is an error in my reasoning? </p>
  
  <p>EDIT: </p>
  
  <p>Here's my code, as Chaitanya requested:</p>
  
  <p>Entity:</p>
  
  <pre><code>@SuppressWarnings("serial")
  @Entity(name = "t_quiz_groups")
  @DynamicUpdate
  @SelectBeforeUpdate
  public class QuizGroupEntity implements Serializable {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "group_id")
  private long id;
  
  @Column(name = "description", nullable = false)
  private String description;
  
  @Column(name = "create_date", nullable = false)
  @Column(name = "create_date")
  private Calendar createDate;
  
  +setters and getters
  </code></pre>
  
  <p>Then, I have a service (@Transactional) method:</p>
  
  <pre><code>    public void update(long groupId, String newDescription) throws ServiceException {
  
      QuizGroupEntity quizGroupEntity = quizRepository.getGroupById(groupId);
  
      ExpectedNotNull.of(quizGroupEntity, RegistryErrorCodes.QUIZ_GROUP_NOT_EXISTS);
  
      quizGroupEntity.setCreateDate(null); //for testing purposes i am setting another
      field as null (shouldnt be updated then, right?)
  
      quizRepository.lock(quizGroupEntity, LockModeType.WRITE);
  
      quizGroupEntity.updateDescription(newDescription); // new description
  
      quizRepository.merge(quizGroupEntity);
  
  
  }
  </code></pre>
  
  <p>And now, in AbstractEntityPersiste, method:</p>
  
  <pre><code>getPropertiesToUpdate( dirtyFields, hasDirtyCollection );
  </code></pre>
  
  <p>is called, and dirtyFields[] consists of two 'dirty' columns: [0,2] - and that is correct, those two columns were modified (date and description, date set to null)</p>
  
  <p>Then, generateUpdateString( propsToUpdate, j, oldFields, j == 0 &amp;&amp; rowId != null ) is called, with 'propsToUpdate' looking like this:</p>
  
  <pre><code>[true, false, true, false, true]
  </code></pre>
  
  <p>Which is wrong, because indeed first and third columns were modified, but first one is set to null, that is why </p>
  
  <pre><code>generateUpdateString( propsToUpdate, j, oldFields, j == 0 &amp;&amp; rowId != null ) 
  </code></pre>
  
  <p>Generates query:</p>
  
  <pre><code>[update t_quiz_groups set create_date=?, description=?, version=? where group_id=? and version=?]
  </code></pre>
  
  <p>That will modify create_date, and override it with null value.</p>