14

我们正在使用 Hibernate Envers 并且有以下情况:

一个类BusinessObjectType和一个类Identity引用BusinessObjectType

@Entity
@Table( name = "ID_IDENTITY" )
@Audited
public class Identity {

    @ManyToOne
    @JoinColumn( name = "BO_TYPE_ID" )
    @IndexColumn( name = "INDEX_BO_BO_TYPE" )
    private BusinessObjectType businessObjectType;

    […]

}

然后,我们使用以下命令查询所有版本的 Identity:

AuditQuery auditQuery = auditReader.createQuery().forRevisionsOfEntity(
    Identity.class,
    false,
    true );
auditQuery.add( AuditEntity.id().eq( dbid ) );

@SuppressWarnings( "unchecked" )
List< Object[]> history = (List< Object[]>) auditQuery.getResultList();

如果存储的身份没有BusinessObjectType(即businessObjectType是和是空的)一切都像一个魅力。

如果身份有,businessObjectType != null我们会得到“Javassist Enhancement failed”异常:

Javassist Enhancement failed: ch.ethz.id.wai.baseclasses.BusinessObjectType

该错误似乎与尝试实例化 BusinessObjectType 的 Envers 有关,但我并没有真正看到问题所在(如果我们不使用 AuditQuery,Hibernate 对这两个对象都没有问题)。

异常的原因是

java.lang.InstantiationException: ch.ethz.id.wai.baseclasses.BusinessObjectType_$$_javassist_49

没有堆栈跟踪。

关于问题可能是什么的任何提示?

4

2 回答 2

21

这发生在以下类中JavassistLazyInitializer 基于 Javassist 的惰性初始化器代理。

如果不查看完整源代码,很难发表评论,但您可以尝试以下选项。

  • 关闭 @ManyToOne 关系的延迟加载 [这是一个设计决定,因此请注意它是否不适合整体解决方案]
  • 为您的实体提供一个导致问题的默认公共构造函数[这更容易]
  • 如果不需要,通过将 hibernate.bytecode.use_reflection_optimizer属性设置为 false 来关闭反射优化

让我们知道这是否有帮助

于 2011-09-10T17:38:43.737 回答
1

To get a more information about the exception, use the debugging facilities of your IDE to set an exception breakpoint for java.lang.InstantiationException to halt execution when the underlying exception occurs. This should show you the full stack trace, and allow you to inspect all variables on the stack.

If I had to guess, my first suspicion would be that since the association to BusinessObjectType is not mapped lazy, plain hibernate doesn't ever try to create a proxy for the class. Envers in contrast appears to do. A proxy is a subclass generated at runtime overriding all public methods. Therefore, neither the class nor any public methods (beside those inherited from Object) may be declared final, and a default constructor must be accessible to the subclass.

于 2011-09-11T06:44:00.083 回答