I am using the Hibernate 4.3.6 and I made use of the latest Maven bytecode enhancement to instrument all entities for self dirtiness awareness.
I added the maven plugin:
Hibernate 5 fixes this issue and now the dirty checking for a setter looks like this:
public void $$_hibernate_write_title(String paramString)
{
if (!EqualsHelper.areEqual(this.title, paramString)) {
$$_hibernate_trackChange("title");
}
this.title = paramString;
}
public void $$_hibernate_trackChange(String paramString)
{
if (this.$$_hibernate_tracker == null) {
this.$$_hibernate_tracker = new SimpleFieldTracker();
}
this.$$_hibernate_tracker.add(paramString);
}
So, the solution is an upgrade to Hibernate 5.
I don't know if it will give you the correct behaviour in all situations, but you can generally get the dirty checks working (at least according to some skeleton code I tested it with) by doing the following:
@EntityListeners(YourListener.class)
to the entities@Pre
/@Post
(eg @PrePersist
etc) methods to your YourListener.class
where you check if the entity is an instance of PersistentAttributeInterceptable
, and if it is just call $$_hibernate_setInterceptor
on it with a custom PersistentAttributeInterceptor
that just returns the new values (that particular behaviour may need refined for general use, i'm not sure, but it was good enough to catch it for my simple tests - you know more about general use cases for the interceptor than me).A hack solution for what is clearly a bug.