# Envers is not working in a multi tenant configuration

**URL:** <https://discourse.hibernate.org/t/envers-is-not-working-in-a-multi-tenant-configuration/8416>\
**Category:** Hibernate ORM\
**Created:** [October 18, 2023, 10:39pm UTC](https://discourse.hibernate.org/t/envers-is-not-working-in-a-multi-tenant-configuration/8416 "2023-10-18T22:39:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmiguel77](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/jmiguel77/32/2718_2.png) [@jmiguel77](https://discourse.hibernate.org/u/jmiguel77)\
**Post date:** [October 18, 2023, 10:39pm UTC](https://discourse.hibernate.org/t/envers-is-not-working-in-a-multi-tenant-configuration/8416/1 "2023-10-18T22:39:15Z")

</div>

Hi  
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

```auto
@Inject
protected EntityManager em;

```

and with a producer method like this

```auto
@Produces
@Default
@RequestScoped
public EntityManager create() {
        String principalName = principal.getName();
        String clientIdentifier = getClientIdentifier(principalName);
        MultiTenantResolver tenantResolver = (MultiTenantResolver) ((SessionFactoryImplementor) entityManagerFactory).getCurrentTenantIdentifierResolver();
        tenantResolver.setTenantIdentifier(clientIdentifier);
        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

```auto
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

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 19, 2023, 12:39pm UTC](https://discourse.hibernate.org/t/envers-is-not-working-in-a-multi-tenant-configuration/8416/2 "2023-10-19T12:39:37Z")

</div>

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](https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#defaults-for-implicit-sequence-generators)

---

<div class="post-metadata">

**Author:** ![jmiguel77](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/jmiguel77/32/2718_2.png) [@jmiguel77](https://discourse.hibernate.org/u/jmiguel77)\
**Post date:** [October 19, 2023, 5:24pm UTC](https://discourse.hibernate.org/t/envers-is-not-working-in-a-multi-tenant-configuration/8416/3 "2023-10-19T17:24:21Z")

</div>

Thanks a lot, it worked great
