title: Hibernate 4 bytecode enhancement not working for dirty checking optimization
body: <p>I am using the Hibernate 4.3.6 and I made use of the latest <a href="http://vladmihalcea.com/hibernate-4-bytecode-enhancement/" rel="nofollow noreferrer">Maven bytecode enhancement</a> to instrument all entities for self dirtiness awareness.</p>
  
  <p>I added the maven plugin:</p>
  
  <pre><code>&lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.hibernate.orm.tooling&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-enhance-maven-plugin&lt;/artifactId&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;phase&gt;process-test-resources&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;enhance&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>and I see my entities are being enhanced:</p>
  
  <pre><code>@Entity
  public class EnhancedOrderLine
  implements ManagedEntity, PersistentAttributeInterceptable, SelfDirtinessTracker
  {
      @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private Long number;
    private String orderedBy;
    private Date orderedOn;
  
    @Transient
    private transient PersistentAttributeInterceptor $$_hibernate_attributeInterceptor;
  
    @Transient
    private transient Set $$_hibernate_tracker;
  
    @Transient
    private transient CollectionTracker $$_hibernate_collectionTracker;
  
    @Transient
    private transient EntityEntry $$_hibernate_entityEntryHolder;
  
    @Transient
    private transient ManagedEntity $$_hibernate_previousManagedEntity;
  
    @Transient
    private transient ManagedEntity $$_hibernate_nextManagedEntity;
  
    ...
  </code></pre>
  
  <p>While debugging, I am checking <code>org.hibernate.event.internal.DefaultFlushEntityEventListener#dirtyCheck</code> method:</p>
  
  <pre><code>        if ( entity instanceof SelfDirtinessTracker ) {
              if ( ( (SelfDirtinessTracker) entity ).$$_hibernate_hasDirtyAttributes() ) {
                  dirtyProperties = persister.resolveAttributeIndexes( ( (SelfDirtinessTracker) entity ).$$_hibernate_getDirtyAttributes() );
              }
          }
  </code></pre>
  
  <p>and the <code>$$_hibernate_hasDirtyAttributes()</code> always returns <strong>false</strong>.</p>
  
  <p>This is because <code>$$_hibernate_attributeInterceptor</code> is always null, so when setting any property:</p>
  
  <pre><code>private void $$_hibernate_write_number(Long paramLong)
  {
   if (($$_hibernate_getInterceptor() == null) || ((this.number == null) || (this.number.equals(paramLong))))
    break label39;
   $$_hibernate_trackChange("number");
   label39: Long localLong = paramLong;
   if ($$_hibernate_getInterceptor() != null)
    localLong = (Long)$$_hibernate_getInterceptor().writeObject(this, "number", this.number, paramLong);
   this.number = localLong;
  }
  </code></pre>
  
  <p>because the <code>$$_hibernate_getInterceptor()</code> is null the trackChange will be bypassed, hence the bytecode enhancement will not resolve the dirty properties and the default deep-comparison algorithm will be used.</p>
  
  <p>What am I missing? How can I get the <code>$$_hibernate_attributeInterceptor</code> to be properly set so that the dirty properties are tracked by the bytecode instrumentation methods?</p>