Hibernate Bytecode Enhancement
Introduction
Now that you know the basics of Hibernate dirty checking, we can dig into enhanced dirty checking mechanisms. While the default graph-traversal algorithm might be sufficient for most use-cases, there might be times when you need an optimized dirty checking algorithm and instrumentation is much more convenient than building your own custom strategy.
Using Ant Hibernate Tools
Traditionally, The Hibernate Tools have been focused on Ant and Eclipse. Bytecode instrumentation has been possible since Hibernate 3, but it required an Ant task to run the CGLIB or Javassist bytecode enhancement routines.
Maven supports running Ant tasks through the maven-antrun-plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>Instrument domain classes</id>
<configuration>
<tasks>
<taskdef name="instrument"
classname="org.hibernate.tool.instrument.javassist.InstrumentTask">
<classpath>
<path refid="maven.dependency.classpath"/>
<path refid="maven.plugin.classpath"/>
</classpath>
</taskdef>
<instrument verbose="true">
<fileset dir="${project.build.outputDirectory}">
<include name="**/flushing/*.class"/>
</fileset>
</instrument>
</tasks>
</configuration>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
So for the following entity source class:
@Entity
public class EnhancedOrderLine {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long number;
private String orderedBy;
private Date orderedOn;
public Long getId() {
return id;
}
public Long getNumber() {
return number;
}
public void setNumber(Long number) {
this.number = number;
}
public String getOrderedBy() {
return orderedBy;
}
public void setOrderedBy(String orderedBy) {
this.orderedBy = orderedBy;
}
public Date getOrderedOn() {
return orderedOn;
}
public void setOrderedOn(Date orderedOn) {
this.orderedOn = orderedOn;
}
}
During build-time the following class is generated:
@Entity
public class EnhancedOrderLine implements FieldHandled {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private Long number;
private String orderedBy;
private Date orderedOn;
private transient FieldHandler $JAVASSIST_READ_WRITE_HANDLER;
public Long getId() {
return $javassist_read_id();
}
public Long getNumber() {
return $javassist_read_number();
}
public void setNumber(Long number) {
$javassist_write_number(number);
}
public String getOrderedBy() {
return $javassist_read_orderedBy();
}
public void setOrderedBy(String orderedBy) {
$javassist_write_orderedBy(orderedBy);
}
public Date getOrderedOn() {
return $javassist_read_orderedOn();
}
public void setOrderedOn(Date orderedOn) {
$javassist_write_orderedOn(orderedOn);
}
public FieldHandler getFieldHandler() {
return this.$JAVASSIST_READ_WRITE_HANDLER;
}
public void setFieldHandler(FieldHandler paramFieldHandler) {
this.$JAVASSIST_READ_WRITE_HANDLER = paramFieldHandler;
}
public Long $javassist_read_id() {
if (getFieldHandler() == null)
return this.id;
}
public void $javassist_write_id(Long paramLong) {
if (getFieldHandler() == null) {
this.id = paramLong;
return;
}
this.id = ((Long)getFieldHandler().writeObject(this, "id", this.id, paramLong));
}
public Long $javassist_read_number() {
if (getFieldHandler() == null)
return this.number;
}
public void $javassist_write_number(Long paramLong) {
if (getFieldHandler() == null) {
this.number = paramLong;
return;
}
this.number = ((Long)getFieldHandler().writeObject(this, "number", this.number, paramLong));
}
public String $javassist_read_orderedBy() {
if (getFieldHandler() == null)
return this.orderedBy;
}
public void $javassist_write_orderedBy(String paramString) {
if (getFieldHandler() == null) {
this.orderedBy = paramString;
return;
}
this.orderedBy = ((String)getFieldHandler().writeObject(this, "orderedBy", this.orderedBy, paramString));
}
public Date $javassist_read_orderedOn() {
if (getFieldHandler() == null)
return this.orderedOn;
}
public void $javassist_write_orderedOn(Date paramDate) {
if (getFieldHandler() == null) {
this.orderedOn = paramDate;
return;
}
this.orderedOn = ((Date)getFieldHandler().writeObject(this, "orderedOn", this.orderedOn, paramDate));
}
}
Although the org.hibernate.bytecode.instrumentation.spi.AbstractFieldInterceptormanages to intercept dirty fields, this info is never really enquired during dirtiness tracking.
The InstrumentTask bytecode enhancement can only tell whether an entity is dirty, lacking support for indicating which properties have been modified, therefore making the InstrumentTask more suitable for “No-proxy” LAZY fetching strategy.
If you enjoy reading this article, you might want to subscribe to my newsletter and get a discount for my book as well.
hibernate-enhance-maven-plugin
Hibernate 4.2.8 added support for a dedicated Maven bytecode enhancement plugin.
The Maven bytecode enhancement plugin is easy to configure:
<build>
<plugins>
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
During project build-time, the following class is being generated:
@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;
public Long getId() {
return $$_hibernate_read_id();
}
public Long getNumber() {
return $$_hibernate_read_number();
}
public void setNumber(Long number) {
$$_hibernate_write_number(number);
}
public String getOrderedBy() {
return $$_hibernate_read_orderedBy();
}
public void setOrderedBy(String orderedBy) {
$$_hibernate_write_orderedBy(orderedBy);
}
public Date getOrderedOn() {
return $$_hibernate_read_orderedOn();
}
public void setOrderedOn(Date orderedOn) {
$$_hibernate_write_orderedOn(orderedOn);
}
public PersistentAttributeInterceptor $$_hibernate_getInterceptor() {
return this.$$_hibernate_attributeInterceptor;
}
public void $$_hibernate_setInterceptor(PersistentAttributeInterceptor paramPersistentAttributeInterceptor) {
this.$$_hibernate_attributeInterceptor = paramPersistentAttributeInterceptor;
}
public void $$_hibernate_trackChange(String paramString) {
if (this.$$_hibernate_tracker == null)
this.$$_hibernate_tracker = new HashSet();
if (!this.$$_hibernate_tracker.contains(paramString))
this.$$_hibernate_tracker.add(paramString);
}
private boolean $$_hibernate_areCollectionFieldsDirty() {
return ($$_hibernate_getInterceptor() != null) && (this.$$_hibernate_collectionTracker != null);
}
private void $$_hibernate_getCollectionFieldDirtyNames(Set paramSet) {
if (this.$$_hibernate_collectionTracker == null)
return;
}
public boolean $$_hibernate_hasDirtyAttributes() {
return ((this.$$_hibernate_tracker == null) || (this.$$_hibernate_tracker.isEmpty())) && ($$_hibernate_areCollectionFieldsDirty());
}
private void $$_hibernate_clearDirtyCollectionNames() {
if (this.$$_hibernate_collectionTracker == null)
this.$$_hibernate_collectionTracker = new CollectionTracker();
}
public void $$_hibernate_clearDirtyAttributes() {
if (this.$$_hibernate_tracker != null)
this.$$_hibernate_tracker.clear();
$$_hibernate_clearDirtyCollectionNames();
}
public Set<String> $$_hibernate_getDirtyAttributes() {
if (this.$$_hibernate_tracker == null)
this.$$_hibernate_tracker = new HashSet();
$$_hibernate_getCollectionFieldDirtyNames(this.$$_hibernate_tracker);
return this.$$_hibernate_tracker;
}
private Long $$_hibernate_read_id() {
if ($$_hibernate_getInterceptor() != null)
this.id = ((Long) $$_hibernate_getInterceptor().readObject(this, "id", this.id));
return this.id;
}
private void $$_hibernate_write_id(Long paramLong) {
if (($$_hibernate_getInterceptor() == null) || ((this.id == null) || (this.id.equals(paramLong))))
break label39;
$$_hibernate_trackChange("id");
label39:
Long localLong = paramLong;
if ($$_hibernate_getInterceptor() != null)
localLong = (Long) $$_hibernate_getInterceptor().writeObject(this, "id", this.id, paramLong);
this.id = localLong;
}
private Long $$_hibernate_read_number() {
if ($$_hibernate_getInterceptor() != null)
this.number = ((Long) $$_hibernate_getInterceptor().readObject(this, "number", this.number));
return this.number;
}
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;
}
private String $$_hibernate_read_orderedBy() {
if ($$_hibernate_getInterceptor() != null)
this.orderedBy = ((String) $$_hibernate_getInterceptor().readObject(this, "orderedBy", this.orderedBy));
return this.orderedBy;
}
private void $$_hibernate_write_orderedBy(String paramString) {
if (($$_hibernate_getInterceptor() == null) || ((this.orderedBy == null) || (this.orderedBy.equals(paramString))))
break label39;
$$_hibernate_trackChange("orderedBy");
label39:
String str = paramString;
if ($$_hibernate_getInterceptor() != null)
str = (String) $$_hibernate_getInterceptor().writeObject(this, "orderedBy", this.orderedBy, paramString);
this.orderedBy = str;
}
private Date $$_hibernate_read_orderedOn() {
if ($$_hibernate_getInterceptor() != null)
this.orderedOn = ((Date) $$_hibernate_getInterceptor().readObject(this, "orderedOn", this.orderedOn));
return this.orderedOn;
}
private void $$_hibernate_write_orderedOn(Date paramDate) {
if (($$_hibernate_getInterceptor() == null) || ((this.orderedOn == null) || (this.orderedOn.equals(paramDate))))
break label39;
$$_hibernate_trackChange("orderedOn");
label39:
Date localDate = paramDate;
if ($$_hibernate_getInterceptor() != null)
localDate = (Date) $$_hibernate_getInterceptor().writeObject(this, "orderedOn", this.orderedOn, paramDate);
this.orderedOn = localDate;
}
public Object $$_hibernate_getEntityInstance() {
return this;
}
public EntityEntry $$_hibernate_getEntityEntry() {
return this.$$_hibernate_entityEntryHolder;
}
public void $$_hibernate_setEntityEntry(EntityEntry paramEntityEntry) {
this.$$_hibernate_entityEntryHolder = paramEntityEntry;
}
public ManagedEntity $$_hibernate_getPreviousManagedEntity() {
return this.$$_hibernate_previousManagedEntity;
}
public void $$_hibernate_setPreviousManagedEntity(ManagedEntity paramManagedEntity) {
this.$$_hibernate_previousManagedEntity = paramManagedEntity;
}
public ManagedEntity $$_hibernate_getNextManagedEntity() {
return this.$$_hibernate_nextManagedEntity;
}
public void $$_hibernate_setNextManagedEntity(ManagedEntity paramManagedEntity) {
this.$$_hibernate_nextManagedEntity = paramManagedEntity;
}
}
It’s easy to realize that the new bytecode enhancement logic is different than the one generated by the previous InstrumentTask.
Like the custom dirty checking mechanism, the new bytecode enhancement version records what properties have changed, not just a simple dirty boolean flag. The enhancement logic marks dirty fields upon changing. This approach is much more efficient than having to compare all current property values against the load-time snapshot data.
If you enjoyed this article, I bet you are going to love my book as well.
Are we there yet?
Even if the entity class bytecode is being enhanced, somehow with Hibernate 4.3.6there are still missing puzzle pieces.
For instance, when calling setNumber(Long number) the following intercepting method gets executed:
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;
}
In my examples, $$_hibernate_getInterceptor() is always null, which bypasses the$$_hibernate_trackChange(“number”) call. Because of this, no dirty property is going to be recorded, forcing Hibernate to fall-back to the default deep-comparison dirty checking algorithm.
So, even if Hibernate has made considerable progress in this particular area, the dirty checking enhancement still requires additional work to become readily available.
Code available on GitHub.
If you have enjoyed reading my article and you’re looking forward to getting instant email notifications of my latest posts, you just need tofollow my blog.