Hibernate 4 bytecode enhancement not working for dirty checking optimization

后端 未结 2 1284
星月不相逢
星月不相逢 2021-02-02 08:07

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:

相关标签:
2条回答
  • 2021-02-02 08:22

    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.

    0 讨论(0)
  • 2021-02-02 08:45

    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.

    0 讨论(0)
提交回复
热议问题