title: Hibernate @PreUpdate: check what has been changed
body: <p><strong>Question:</strong> <em>How to check which fields has been changed inside method annotated with <code>@PreUpdate</code>?</em></p>
  
  <hr>
  
  <p><em>OPTIONAL: if the answer to the question above is "It's impossible, than maybe there are another ways to solve my problem"</em></p>
  
  <p><strong>I want</strong> automatically update <code>modified</code> <code>Tourist</code>'s field each time we change something in it. </p>
  
  <p><strong>Except the situation</strong> when we modify only <code>location</code>. Means if we change <code>location</code> only - it should be persisted, but <code>modified</code> mustn't be changed.</p>
  
  <p><strong>Already present code:</strong></p>
  
  <pre><code>@Entity
  public class Tourist {
  
    private long id;
    private String firstName;
    private String lastName;
    private Date created;
    private Date modified;
    private String location;
  
    @PreUpdate
    public void preUpdate() {
       modified = new Date(); //PROBLEM : change modified even if only location field has been changed!
    }   
    ....
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Updated:</strong> After some investigations I found that I can solve it with help of interceptors (extend <code>EmptyInterceptor</code>):</p>
  
  <pre><code>public class TouristInterceptor extends EmptyInterceptor{
  
      Session session;
      private Set updates = new HashSet();
  
      public void setSession(Session session) {
          this.session=session;
      }
  
      public boolean onFlushDirty(Object entity,Serializable id,
          Object[] currentState,Object[] previousState,
          String[] propertyNames,Type[] types)
          throws CallbackException {
  
          if (entity instanceof Tourist){
              if (somethingChangedExceptLocation()) 
                  updates.add(entity);
          }
          return false;
      }
  </code></pre>
  
  <p>But disadvantage of this approach is to intercept everything when you need to intercept the single entity.</p>
  
  <p><strong>Updated Questions:</strong></p>
  
  <ul>
  <li>How to intercept only Tourist entity flush calls?</li>
  <li>Is that possible to do the same with help of events? Means <code>PreUpdateEvent</code> which contains new and old state</li>
  </ul>