【发布时间】:2014-09-06 16:34:19
【问题描述】:
我使用的是 Hibernate 4.3.6,并使用了最新的 Maven bytecode enhancement 来检测所有实体的自我肮脏意识。
我添加了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() 为空,trackChange 将被绕过,因此字节码增强不会解决脏属性,将使用默认的深度比较算法。
我错过了什么?如何正确设置$$_hibernate_attributeInterceptor,以便字节码检测方法跟踪脏属性?
【问题讨论】:
-
那个增强是字节码增强你是怎么得到EnhancedOrderLine类的源代码的。
-
关注问题开头的 JIRA 问题链接。
-
当我查看您提到的链接时,我看到那些人在编译阶段使用它。你能在编译测试阶段再试一次吗?
-
好的。我会试一试,然后回复你。
-
那是因为我也写了那篇文章 ;)
标签: java hibernate orm byte-code-enhancement dirty-checking