Envers is not working in a multi tenant configuration

I am trying to migrate an application to use multi tenant. My application is running in wildfly 29, with jakarta ee 10.

I am providing the EntityManager using CDI injection like this

protected EntityManager em;

and with a producer method like this

public EntityManager create() {
        String principalName = principal.getName();
        String clientIdentifier = getClientIdentifier(principalName);
        MultiTenantResolver tenantResolver = (MultiTenantResolver) ((SessionFactoryImplementor) entityManagerFactory).getCurrentTenantIdentifierResolver();
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        return entityManager;

This injection is working fine when i am trying to retrieve information; a user logs in the application and the EntityManager is created, connected to the right database for that user

But when i try to perform a operation that audits data, this exception is thrown

org.hibernate.envers.exception.AuditException: Negative revision numbers are not allowed
	at org.hibernate@6.2.6.Final//org.hibernate.envers.internal.revisioninfo.DefaultRevisionInfoGenerator.saveRevisionData(DefaultRevisionInfoGenerator.java:64)
	at org.hibernate@6.2.6.Final//org.hibernate.envers.internal.synchronization.AuditProcess.getCurrentRevisionData(AuditProcess.java:138)

Taking a look at the source code of the org.hibernate.envers.internal.revisioninfo.DefaultRevisionInfoGenerator i see that the revisionData object arrives with an id = 0, but after this operation:
session.save(this.revisionInfoEntityName, revisionData);
the id changes to a negative number

any help is greatly appreciated

Without knowing too much about you application, negative identifier values usually means that you did not setup the sequence correctly. See https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#defaults-for-implicit-sequence-generators

1 Like

Thanks a lot, it worked great