我使用 envers 的时间不长,而且遇到了一个问题,我似乎无法在任何地方找到答案。我尝试过谷歌搜索堆栈跟踪但无济于事,所以我一定错过了一些非常明显的东西。
每当我尝试在 org.hibernate.envers.AuditReader 中运行“find(class, versionNumber)”方法以检索数据库中 Document 类的先前版本时,我似乎都会得到以下堆栈跟踪:
java.lang.NullPointerException
org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:91)
org.hibernate.envers.entities.EntityInstantiator.addInstancesFromVersionsEntities(EntityInstantiator.java:113)
org.hibernate.envers.query.impl.EntitiesAtRevisionQuery.list(EntitiesAtRevisionQuery.java:110)
org.hibernate.envers.query.impl.AbstractAuditQuery.getSingleResult(AbstractAuditQuery.java:108)
org.hibernate.envers.reader.AuditReaderImpl.find(AuditReaderImpl.java:119)
org.hibernate.envers.reader.AuditReaderImpl.find(AuditReaderImpl.java:94)
com.example.repository.AbstractDocumentTypeManager.getVersion(AbstractDocumentTypeManager.java:221)
com.example.repository.AbstractDocumentTypeManager.getVersion(AbstractDocumentTypeManager.java:1)
com.example.logic.versioning.BaseVersioningService.getApprovedSnapshot(BaseVersioningService.java:54)
com.example.logic.versioning.BaseVersioningService$$FastClassByCGLIB$$d33887d.invoke(<generated>)
net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
...
导致的代码这个错误如下。它发生在我的文档保存后,似乎正在数据库中创建适当的版本(document_AUD)。
public Document getVersion(String objectId, Number version) {
AuditReader auditReader = getAuditReader();
/* at this point, i guarantee that neither objectId nor version are null. */
Document revision = auditReader.find(Document.class, objectId, version);
return revision;
}
public AuditReader getAuditReader() {
return AuditReaderFactory.get(entityManager);
}
我的“文档”类注释如下...
@Entity(name = "document")
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@Cacheable(true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "modelObjectCache")
public abstract class Document<T extends Document<?>> extends ModelObject implements Serializable, Comparable<T>,
Versionable {
/* many properties of a Document here. There are 2 subclasses of Document, identified by a database discriminator. */
}
请让我知道您需要更多代码才能正确找到此问题。
非常感谢您的阅读,欢迎提出任何建议,
丰富。
I have not been using envers for very long, and i have hit a problem i just cant seem to find the answer to anywhere. I have tried googling the stack trace but to no avail, so i must be missing something blindingly obvious.
I seem to get the following stack trace whenever i try to run the 'find(class, versionNumber)' method in org.hibernate.envers.AuditReader in order to retrieve a previous version of a Document class in the database:
java.lang.NullPointerException
org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:91)
org.hibernate.envers.entities.EntityInstantiator.addInstancesFromVersionsEntities(EntityInstantiator.java:113)
org.hibernate.envers.query.impl.EntitiesAtRevisionQuery.list(EntitiesAtRevisionQuery.java:110)
org.hibernate.envers.query.impl.AbstractAuditQuery.getSingleResult(AbstractAuditQuery.java:108)
org.hibernate.envers.reader.AuditReaderImpl.find(AuditReaderImpl.java:119)
org.hibernate.envers.reader.AuditReaderImpl.find(AuditReaderImpl.java:94)
com.example.repository.AbstractDocumentTypeManager.getVersion(AbstractDocumentTypeManager.java:221)
com.example.repository.AbstractDocumentTypeManager.getVersion(AbstractDocumentTypeManager.java:1)
com.example.logic.versioning.BaseVersioningService.getApprovedSnapshot(BaseVersioningService.java:54)
com.example.logic.versioning.BaseVersioningService$FastClassByCGLIB$d33887d.invoke(<generated>)
net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
...
The code which causes this error is below. It takes place after my Document is saved and it seems that the appropriate version is being created in the database (document_AUD).
public Document getVersion(String objectId, Number version) {
AuditReader auditReader = getAuditReader();
/* at this point, i guarantee that neither objectId nor version are null. */
Document revision = auditReader.find(Document.class, objectId, version);
return revision;
}
public AuditReader getAuditReader() {
return AuditReaderFactory.get(entityManager);
}
My 'Document' class is annotated like so...
@Entity(name = "document")
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@Cacheable(true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "modelObjectCache")
public abstract class Document<T extends Document<?>> extends ModelObject implements Serializable, Comparable<T>,
Versionable {
/* many properties of a Document here. There are 2 subclasses of Document, identified by a database discriminator. */
}
Please let me know you need any more code in order to properly find this problem.
Many thanks for reading and any suggestions are welcome,
rich.
发布评论
评论(1)
好吧,我似乎找到了答案 - 当您在 AuditReader 上运行 find 方法时,您必须实际发送实现类而不是抽象超类。
所以,例如,在我说的地方,
我实际上需要输入:
Marvellous。
Alright, i seem to have found the answer - when you run the find method on AuditReader, you have to actually send the implementing class rather than the abstract superclass.
so, for example where i say,
i actually need to put:
Marvellous.