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.
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.