繁体   English   中英

Hibernate 4字节码增强功能不适用于脏检查优化

[英]Hibernate 4 bytecode enhancement not working for dirty checking optimization

我正在使用Hibernate 4.3.6,我利用最新的Maven字节码增强功能来检测所有实体的自我肮脏意识。

我添加了maven插件:

<build>
    <plugins>
        <plugin>
            <groupId>org.hibernate.orm.tooling</groupId>
            <artifactId>hibernate-enhance-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我看到我的实体正在增强:

@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;

  ...

在调试时,我正在检查org.hibernate.event.internal.DefaultFlushEntityEventListener#dirtyCheck方法:

        if ( entity instanceof SelfDirtinessTracker ) {
            if ( ( (SelfDirtinessTracker) entity ).$$_hibernate_hasDirtyAttributes() ) {
                dirtyProperties = persister.resolveAttributeIndexes( ( (SelfDirtinessTracker) entity ).$$_hibernate_getDirtyAttributes() );
            }
        }

并且$$_hibernate_hasDirtyAttributes()始终返回false

这是因为$$_hibernate_attributeInterceptor始终为null,因此在设置任何属性时:

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;
}

因为$$_hibernate_getInterceptor()为null,所以会绕过trackChange,因此字节码增强不会解析脏属性,并且将使用默认的深度比较算法。

我错过了什么? 如何正确设置$$_hibernate_attributeInterceptor ,以便字节码检测方法跟踪脏属性?

Hibernate 5修复了这个问题 ,现在对setter的脏检查如下所示:

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);
}

因此,解决方案是升级到Hibernate 5。

我不知道它是否会在所有情况下都给你正确的行为,但你通常可以通过执行以下操作来使脏检查工作(至少根据我测试过的一些框架代码):

  1. 通过将@EntityListeners(YourListener.class)添加到实体来注册实体侦听器
  2. 将所有@Pre / @Post (例如@PrePersist等)方法的实现添加到YourListener.class ,在那里检查实体是否是PersistentAttributeInterceptable的实例,如果它只是使用自定义PersistentAttributeInterceptor调用$$_hibernate_setInterceptor返回新的值(特定的行为可能需要经过精心处理以供一般使用,我不确定,但是对于我的简单测试来说它已经足够好了 - 你对拦截器的一般用例比我更了解)。

什么是明显的错误的黑客解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM