I have an entity loaded by Hibernate (via EntityManager
):
User u = em.load(User.class, id)
This class is audited by Hibernate Envers. How can I load the previous version of a User entity?
I have an entity loaded by Hibernate (via EntityManager
):
User u = em.load(User.class, id)
This class is audited by Hibernate Envers. How can I load the previous version of a User entity?
I think it would be this:
Here's another version that finds the previous revision relative to a "current" revision number, so it can be used even if the entity you're looking at isn't the latest revision. It also handles the case where there isn't a prior revision. (
em
is assumed to be a previously-populated EntityManager)This can be generalized to:
The only tricky bit with this generalization is getting the entity's id. Because I'm using the Play! framework, I can exploit the fact that all entities are Models and use
((Model) entity).id
to get the id, but you'll have to adjust this to suit your environment.From the docs:
Building off of the excellent approach of @brad-mace, I have made the following changes:
So here's another solution:
maybe this then (from AuditReader docs)
(I'm very new to this, not sure if I have all the syntax right, maybe the size()-1 should be size()-2?)