Get previous version of entity in Hibernate Envers

后端 未结 5 1751
孤街浪徒
孤街浪徒 2021-01-31 22:36

I have an entity loaded by Hibernate (via EntityManager):

User u = em.load(User.class, id)

This class is audited by Hibernate Enve

5条回答
  •  星月不相逢
    2021-01-31 23:30

    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)

    public static User getPreviousVersion(User user, int current_rev) {
        AuditReader reader = AuditReaderFactory.get(em);
    
        Number prior_revision = (Number) reader.createQuery()
        .forRevisionsOfEntity(User.class, false, true)
        .addProjection(AuditEntity.revisionNumber().max())
        .add(AuditEntity.id().eq(user.getId()))
        .add(AuditEntity.revisionNumber().lt(current_rev))
        .getSingleResult();
    
        if (prior_revision != null)
            return (User) reader.find(User.class, user.getId(), prior_revision);
        else
            return null
    }
    

    This can be generalized to:

    public static T getPreviousVersion(T entity, int current_rev) {
        AuditReader reader = AuditReaderFactory.get(JPA.em());
    
        Number prior_revision = (Number) reader.createQuery()
        .forRevisionsOfEntity(entity.getClass(), false, true)
        .addProjection(AuditEntity.revisionNumber().max())
        .add(AuditEntity.id().eq(((Model) entity).id))
        .add(AuditEntity.revisionNumber().lt(current_rev))
        .getSingleResult();
    
        if (prior_revision != null)
            return (T) reader.find(entity.getClass(), ((Model) entity).id, prior_revision);
        else
            return null
    }
    

    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.

提交回复
热议问题