我在尝试让经审计的实体引用未经审计的实体时遇到问题。在我们的应用程序中,某些实体是在不使用休眠的情况下引导的,这些实体是我们的元模型,不需要审计。
工作示例:
public class A {
private String id;
private List<B> attributeReferences;
}
public class B {
private String id;
private A attributeReference;
}
如果我执行代码:
A rev1 = getAuditReader().find(A.class, "foo", 1);
A rev2 = getAuditReader().find(A.class, "foo", 2);
A rev3 = getAuditReader().find(A.class, "foo", 3);
一切顺利,但是如果我尝试获取 B 的版本,则会发生错误:
B rev2 = getAuditReader().find(B.class, "bar", 2);
org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行 [metafoo]
Envers 通过对我的 metafoo 实体的惰性引用成功地查找了 B 实体。但是,当尝试将其存储在其缓存中时,我的 B 实体的 hashcode 方法被调用,这反过来会查找审计表中不存在的 metafoo,从而引发异常。
有没有办法可以忽略那些不存在的引用而不是抛出这个错误?(也许通过在查询中排除此类引用)
如何完全处理不存在的数据(假设我们从审计表中删除了所有超过 1 个月的条目)我们如何仍然查询引用不存在的实体?
PS:
如果可能,我不想修改哈希码函数
我不想审核我的元模型
表结构:
表一:
ID
----------------------------------------
metaFooAttribute
fooAttribute
foo
表 A_AUD:
REV REVTYPE ID
----------------------------------------
1 0 fooAttribute
1 0 foo
2 1 foo
3 1 foo
表 B:
ID
----------------------------------------
bar
表 B_AUD:
REV REVTYPE ID ATTRIBUTE_ID
----------------------------------------
1 0 bar metaFooAttribute
1 0 bar fooAttribute
2 1 bar fooAttribute
堆栈跟踪:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [playground.test.A#metafoo]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:435)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:189)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:178)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:189)
at playground.test.A$$EnhancerByCGLIB$$3cdb0441.hashCode(<generated>)
at playground.test.B.hashCode(B.java:120)
at org.hibernate.envers.tools.Triple.hashCode(Triple.java:73)
at java.util.HashMap.put(HashMap.java:372)
at org.hibernate.envers.reader.FirstLevelCache.putOnEntityNameCache(FirstLevelCache.java:94)
at org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:105)
at org.hibernate.envers.entities.EntityInstantiator.addInstancesFromVersionsEntities(EntityInstantiator.java:113)
at org.hibernate.envers.query.impl.EntitiesAtRevisionQuery.list(EntitiesAtRevisionQuery.java:110)
at org.hibernate.envers.query.impl.AbstractAuditQuery.getSingleResult(AbstractAuditQuery.java:108)
at org.hibernate.envers.reader.AuditReaderImpl.find(AuditReaderImpl.java:119)
at org.hibernate.envers.reader.AuditReaderImpl.find(AuditReaderImpl.java:94)
at playground.test.TestEnvers.testGetAttribute(TestActivityStream.java:60)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
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:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
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)
编辑:
我发现这篇文章很有趣,但它并没有解决问题,因为我的参考是 A 类型,所以注释 @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) 不起作用。