我在尝试将审计实体引用到未审计的实体时遇到问题。在我们的应用程序中,某些实体在不使用hibernate的情况下被引导,这些实体是我们的元模型,不需要被审计。
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实体的哈希码方法被调用,这反过来又会对审计表中不存在的metafoo进行查找,从而引发异常。
有没有办法可以忽略那些不存在的引用而不是抛出这个错误? (可能通过在查询中排除这些引用)
如何完全处理不存在的数据(假设我们从审计表中删除了超过1个月的所有条目)我们怎样才能查询引用不存在的实体?
如果可能的话,我不想修改哈希码函数 我不想审核我的元模型
表A:
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)
我发现这个post很有意思,但它没有解决问题,因为我的引用是A类,所以注释@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)不起作用。
答案 0 :(得分:1)
也许尝试将@NotAudited添加到元字段中。然后该字段将不会被审核,但看起来好像您不想审核它。否则@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)就是你的选择。