In Hibernate Envers, all related collections of an entity are loaded lazily, regardless of what fetch type is set. So when auditquerying for an entity that has a collection of other entities (both audited, of course), the collection is a SetProxy
at first (can be seen when debugging).
So, how do I initialize that proxy? Using Hibernate.initialize()
has no effect (I suspect because Hibernate and Envers are using different proxy objects). I know I can initialize the set by iterating over its items, but that isn't an option for me because I have multiple collections in an entity and not to mention the maintenance issues.
I need to initialize them eagerly because I'm accessing the collection at a later point in time when the Hibernate session is already closed (converting the domain objects into dtos).
I'm using Hibernate 3.5.6.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With