Hibernate 4 bytecode enhancement not working for dirty checking optimization

谁说我不能喝 提交于 2019-12-02 20:12:00

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:

  1. Register an entity listener by adding @EntityListeners(YourListener.class) to the entities
  2. Add implementations for all @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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!