休眠 4 字节代码增强功能不适用于脏检查优化
2022-09-01 07:54:52
我正在使用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() );
}
}
并且 总是返回 false。$$_hibernate_hasDirtyAttributes()
这是因为 始终为 null,因此在设置任何属性时:$$_hibernate_attributeInterceptor
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;
}
因为是空的,所以 trackChange 将被绕过,因此字节码增强不会解析脏属性,并且将使用默认的深度比较算法。$$_hibernate_getInterceptor()
我错过了什么?如何正确设置 ,以便通过字节码检测方法跟踪脏属性?$$_hibernate_attributeInterceptor