Hibernate Envers 6 + Jakarta Data

Hello,
Some background… migrating jakarta appication from

  • hibernate 5->hibernate 6
  • deltaspike → jakarta data

Application uses Envers to audit entities.
Jpamodelgen generates Repositories implementation with StatelessSession

	@PostConstruct
	private void openSession() {
		session = sessionFactory.unwrap(SessionFactory.class).openStatelessSession();
	}
	

and the problem is when entity is saved StatelessSessionImple fires event:

PreInsertEvent event = new PreInsertEvent(entity, id, state, persister, (EventSource)null);

and then:

public class EnversPostInsertEventListenerImpl extends BaseEnversEventListener implements PostInsertEventListener {
	public EnversPostInsertEventListenerImpl(EnversService enversService) {
		super( enversService );
	}

	@Override
	public void onPostInsert(PostInsertEvent event) {
		final String entityName = event.getPersister().getEntityName();

		if ( getEnversService().getEntitiesConfigurations().isVersioned( entityName ) ) {
			checkIfTransactionInProgress( event.getSession() );

all fails with error:

Caused by: java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.spi.SessionImplementor.isTransactionInProgress()" because "session" is null
        at org.hibernate@6.6.3.Final//org.hibernate.envers.event.spi.BaseEnversEventListener.checkIfTransactionInProgress(BaseEnversEventListener.java:134)
        at org.hibernate@6.6.3.Final//org.hibernate.envers.event.spi.EnversPostInsertEventListenerImpl.onPostInsert(EnversPostInsertEventListenerImpl.java:34)
        at org.hibernate@6.6.3.Final//org.hibernate.internal.StatelessSessionImpl.firePostInsert(StatelessSessionImpl.java:380)
        at org.hibernate@6.6.3.Final//org.hibernate.internal.StatelessSessionImpl.insert(StatelessSessionImpl.java:160)
        at org.hibernate@6.6.3.Final//org.hibernate.internal.StatelessSessionImpl.insert(StatelessSessionImpl.java:118)

Is there some solution for this ?

This sounds like a bug in the envers listener, please create a project based on our test case templates that shows the steps needed to reproduce this problem and attach it to a new issue in our tracker.

I doubt that Envers ever worked with StatelessSession.

But on the other hand I guess StatelessSession didn’t used to raise PreInsertEvents, which explains why you started getting NPEs just now.

Ideally, I think, Envers would use its own StatelessSession for persistence operations when the event doesn’t carry a stateful session.

Whatever, an NPE is always a bug, as Marco says.

On 5.x StatelessSession didn’t raise PreInsertEvent. Deltaspike uses stateful Session so Envers works correct.
On jpamodelgen there is no option to use stateful Session