AuditReaderFactory.get(EntityManager entityManager) : StackOverflowError

I wanted to test my application with Payara5 (I use Wildfly otherwise). Payara5 uses EclipseLink.

When the code reaches AuditReaderFactory.get(EntityManager entityManager), instead of throwing an AuditException, it throws a StackOverflowError.

The problem is : entityManager.getDelegate() is always an instance of EntityManager with EclipseLink, so the endless loop.

Could you please protect the code against this ?

Why should we? The alternative is that you get a different exception. Hibernate Envers simply won’t work with EclipseLink, only with Hibernate ORM.

Hi beikov,

At first I thought that it would work with Payara. The method needs only an EntityManager after all.

The method AuditReaderFactory.get(EntityManager entityManager)
should throw AuditException, not StackOverflowError. I surrounded the method with a try-catch AuditException but this obviously doesn’t work.

The method needs only an EntityManager after all.

Well, it does so for convenience because the Hibernate contracts implement the EntityManager interface and we don’t want to force you having to call entityManager.unwrap(Session.class) for retriving the Hibernate related subtype. Either way, it just won’t work with EclipseLink, so why do you care about the exact exception?

I once switched to Payara for testing purposes. It helps to check if everything works. So I would like to desactive properly AuditReaderFactory.get(EntityManager) by catching the AuditException.

So catch the StackOverflowError or check yourself if this is Hibernate that is running by checking if a well known class like e.g. org.hibernate.Hibernate can be loaded.

Catching a StackOverflowError sounds nasty.

I will try Class.forName(" org.hibernate.Hibernate") instead.

Still, it would be nicer to throw the AuditException as promised.

Anyway, thanks for the advice.

I wonder if testing “org.hibernate.Hibernate” is sensible as I have the dependency “hibernate-envers”.