在我的域模型中,我有一个抽象实体Indicator,由两个具体的Elementary和Composite继承
抽象实体持有一个实体Factor,以便两个子类可以访问
这种关系是双向的。因此实体Factor持有抽象实体Indicator的一个实例
当然,实际实例是Elementary或Composite。
@Entity
@Inheritance(strategy = JOINED)
public abstract class Indicator implements Serializable {
@OneToMany(mappedBy = "indicator")
private List<Factor> factors = new ArrayList<Factor>();
...
}
@Entity
@Audited
public class Factor implements Serializable {
@ManyToOne(optional = false)
@JoinColumn(name = "ID_RSK_IND", nullable = false)
@ForeignKey(name = "FK_FAC__IND")
private Indicator indicator;
}
@Entity
@Audited
public class Elementary extends Indicator {
...
}
@Entity
@Audited
public class Composite extends Indicator {
...
}
我使用Dozer将这些实体映射到它们自己,以便"打破"hibernate工具并将它们推送到客户端(GWT)。
对于"经典"的Hibernate,一切都很好:Dozer交叉bean模型来复制它。
但是,当我使用Envers AuditReader查询版本化的实体时,我会得到一个InstantiationException。它来自于Factor的实例试图实例化Indicator 的实例
09:36:04,702 - ERROR - org.dozer.MappingProcessor - Field mapping error -->
MapId: null
Type: null
Source parent class: com.sg.rrf.l2r.shared.entity.market.indicator.elementary.Elementary
Source field name: factors
Source field type: class org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.ListProxy
Source field value: [1]
Dest parent class: com.sg.rrf.l2r.shared.entity.market.indicator.elementary.Elementary
Dest field name: factors
Dest field type: java.util.List
org.dozer.MappingException: java.lang.InstantiationException
at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:82)
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:280)
at org.dozer.factory.ConstructionStrategies$ByConstructor.create(ConstructionStrategies.java:245)
at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:65)
at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:489)
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:446)
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:342)
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:288)
at org.dozer.MappingProcessor.map(MappingProcessor.java:248)
at org.dozer.MappingProcessor.map(MappingProcessor.java:197)
at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:495)
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:446)
at org.dozer.MappingProcessor.addOrUpdateToList(MappingProcessor.java:776)
at org.dozer.MappingProcessor.addOrUpdateToList(MappingProcessor.java:850)
at org.dozer.MappingProcessor.mapListToList(MappingProcessor.java:686)
at org.dozer.MappingProcessor.mapCollection(MappingProcessor.java:541)
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:434)
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:342)
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:288)
at org.dozer.MappingProcessor.map(MappingProcessor.java:248)
at org.dozer.MappingProcessor.map(MappingProcessor.java:197)
at org.dozer.MappingProcessor.map(MappingProcessor.java:187)
at org.dozer.MappingProcessor.map(MappingProcessor.java:124)
at org.dozer.MappingProcessor.map(MappingProcessor.java:119)
at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:120)
at com.sg.rrf.l2r.server.audit.AuditTransactionalBean.getEntityForRevision(AuditTransactionalBean.java:30)
at com.sg.rrf.l2r.server.audit.AuditTransactionalBean$$FastClassByCGLIB$$78958945.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:713)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646)
at com.sg.rrf.l2r.server.audit.AuditTransactionalBean$$EnhancerByCGLIB$$36312869.getEntityForRevision(<generated>)
at com.sg.rrf.l2r.server.audit.AuditServiceImpl.getEntityForRevision(AuditServiceImpl.java:37)
at com.sg.rrf.l2r.server.market.indicator.audit.IndicatorAuditServiceImplTest.assertElementaryValues(IndicatorAuditServiceImplTest.java:120)
at com.sg.rrf.l2r.server.market.indicator.audit.IndicatorAuditServiceImplTest.testAuditElementary(IndicatorAuditServiceImplTest.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:276)
... 74 more
这是因为即使指定了Eager,Envers也使用Lazy加载吗?
PS:当然,我需要从指标到因子的双向导航。
试图将字段factors
映射到Elementary类型的新对象的字段factors
,但此处的属性是一个未知具体类型的List接口。
该字段的映射适用于实际的域模型类型,但不适用于hibernate代理。
是否使用ArrayList初始化factors
?如果不是这样的话,非Envers映射就不会起作用。
这可能与dozer在映射泛型类型(如列表)方面的一些限制有关,因为泛型信息在运行时不可用,所以dozer不知道列表中包含的对象的类型,所以它试图从源集合的内容推断它。
根据推土机文件:
如果没有为目标字段指定提示,则目标集合将填充为与src Collection中的元素类型相同。
因此,要解决此问题,有几种方法:
1-在该属性的映射上放置推土机提示以指定目标类型,这样它就不会试图推断它:
<field>
<a>factors</a>
<b>factors</b>
<b-hint>your.target.class.Here</b-hint>
</field>
2-编写并应用于该属性的Dozer自定义转换器,在手动映射该列表的地方,这将始终有效(使用基于泛型的自定义转换器API)。
3-通过以另一种方式在序列化时解决LazyInitialization,完全避免映射和Dozer的需要:确保hibernate会话一直保持打开,直到请求的序列化,如果在Spring应用程序中,则使用open session in View,否则使用类似方法。
其中一种方法应该可以解决它,如果仍然有疑问,你可以随时:
张贴推土机映射和
factors
类型的代码?使用调试器,您可以在ConstructionStrategies行280中放置一个断点,以查看它试图实例化的抽象类或接口是什么。
这是我的Dozer映射:
protected final static DozerBeanMapper MAPPER = new DozerBeanMapper();
static {
BeanMappingBuilder builder = new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(Elementary.class, Elementary.class);
mapping(Composite.class, Composite.class);
}
};
MAPPER.addMapping(builder);
}
得益于这种映射,Dozer可以映射Hibernate代理的bean
但是使用此映射,Dozer无法映射Envers代理的bean。
我不认为这个问题是由于集合映射造成的。当我在Instaunction上放一个breakPoint时,我使用的是org.dozer.factory.ConstructionStrategies.newInstance(Class-clazz)方法。clazz参数是Indicator.class。
此外,假设我不使用Dozer,并且我在DAO:中调用了这段代码
Indicator indicator = elementary.getFactors().get(0).getIndicator();
System.out.println(indicator.getClass().getSimpleName());
它打印"Indicator_$$_javassist_6"。
测试
indicator instanceof Elementary
或
indicator instanceof Composite
返回false。
使用经典Hibernate(不带Envers)的相同测试为其中一个返回true,并打印正确的类名(即使ManyToOne指示符属性上有FetchType.LAZY!)