Hibernate Envers: Initializing Envers Proxies

The best workaround I've found so far for initializing the Envers proxies is to use Dozer. Mapping the audited entity returned by Envers to itself forces the initialization.

For example:

    // Assuming you have an initialized EntityManager in entityManager & 
    // id contains your entity id..

    List<Object[]> auditList = (List<Object[]>)AuditReaderFactory.
                                   get(entityManager).
                                   createQuery().
                                   forRevisionsOfEntity(Foo.class, false, true).
                                   add(AuditEntity.id().eq(id)).
                                   getResultList();

    // Use a singleton in production apps instead...
    DozerBeanMapper mapper = new DozerBeanMapper();

    for(Object[] audit : auditList) {
        audit[0] = mapper.map(audit[0], Foo.class);
    }

    // The proxies in the Foo instances in auditList are now initialized

I'm not very happy with this solution, but I prefer it over initializing the proxies by manually touching the collections. Hope someone comes up with a better alternative or HHH-3552 gets fixed!


Apparently, this is an open issue with Hibernate Envers. There is already an existing issue in their JIRA: https://hibernate.atlassian.net/browse/HHH-3552. Feel free to vote on it, maybe it will speed things up, when they see that there are some people wanting this to be fixed ;)

Until the Envers team fixes this issue, there is a work around which works for me: Calling size() on the collections initializes the proxy objects.