title: How to make Hibernate @DynamicUpdate work with two updating threads?
body: <p>I'd like to use @DynamicUpdate on my @Entity because I have two threads that change different columns on the same row and, without dynamic query generation, all columns are written on each update and the second thread overwrites what was written by the first one.</p>
  
  <p>Example:</p>
  
  <pre><code>@Entity Task has attributes A and B set to false.
  - Thread 1 loads Task#1 from the database and starts some long operation outside transaction
  - Thread 2 loads Task#1 from the database and sets A=true, then saves
  &gt; the database holds Task#1 with A=true and B=false
  - Thread 1 sets B=true, then saves
  &gt; the database holds Task#1 with A=false (!!!) and B=true
  I was expecting to have A=true but it was overwritten with the 
  original value when Thread 1 saved
  </code></pre>
  
  <p>The problem is that just adding @DynamicUpdate on the @Entity doesn't change the query, which is still updating all columns.
  <strong>UPDATE</strong>: I managed to make this work by annotating all the class hierarchy with @DynamicUpdate (not just the base class holding the attribute as I did before) but I still have problems (see below)</p>
  
  <p>I'm using Spring Data JPA 1.10.2 with Spring 4.2.6 and Hibernate 5.1.0.
  One thread is fetching the entity with a custom find on a JpaRepository, the other one with a findOne() on the same JpaRepository.
  Both call save() on the JpaRepository at the end.
  Only the JpaRepository is @Transactional, so the read and save operations occur in different transactions.</p>
  
  <p>I also tried adding @SelectBeforeUpdate but it didn't make any difference.</p>
  
  <p>Also, is there any documentation on how to use dynamic query generation? I only found two lines in the <a href="https://docs.jboss.org/hibernate/orm/5.1/javadocs/org/hibernate/annotations/DynamicUpdate.html" rel="nofollow">javadoc</a> but nothing in the user guide.</p>
  
  <p>Or maybe I should use a different approach altogether? Any workaround? I can only think of updating each column separately with native sql, but that would be sad.</p>
  
  <p><strong>Update:</strong>
  Now I have dynamic updates, so that only changed columns are saved, in theory.
  But in practice the column that is marked as changed on one thread gets stored by the second thread too!
  This is what happens in more detail:</p>
  
  <p>My Entity is called Task and has "id", "priority", "executionTime" and "foo" attributes.
  It is subclassed twice: BigTask extends Task and HugeTask extends BigTask, but I'm using Task when handling instances.
  All classes are annotated with @Entity and @DynamicUpdate.
  In the "setPriority()" method I've added a debug line that prints the previous and new priority value.</p>
  
  <p>Using the web console, I start a Task instance: a new async thread is started that loads the Task from the database and starts some long-running operation outside of the transaction.
  While the task is running, I change its priority on the web console and save. I see the debug message "Priority changed from 10 to 20" and the database holds the new value. The update query logged by hibernate is </p>
  
  <pre><code>update Task set priority=? where id=?
  </code></pre>
  
  <p>which is correct and shows that @DynamicUpdate is working fine.
  After a couple of minutes the async thread terminates its task, sets the executionTime and saves. The hibernate query is now</p>
  
  <pre><code>update Task set executionTime=?, priority=? where id=?
  </code></pre>
  
  <p>which is NOT as expected. @DynamicUpdate is being used (the "foo" attribute is not included in the query) but "priority" has apparently been marked as modified even if its setter hasn't been called. And it is never accessed directly in the code.</p>
  
  <p>Looks quite weird to me: the Task instances used by the two threads are different (or the new priority would be set on both updates) but apparently the "dirty flag" is in common! Could that really be the case?</p>