Is it possible to request the name of an audit table for a entity from Hibernate envers? I don\'t mean the solution of getting Hibernate table name for entity and appending the
In Hibernate Envers 5.x, there is no centralized way to obtain this information so you'll have to consult several APIs to get this metadata. But there is HHH-12014 and HHH-12044 that highlight the need to expose this so that it can be consumed by user code easier.
In order to gain access to this information, we'll need to first obtain some SPIs that are only available via the SessionImplementor
interface.
I must stress this approach can change with each release as some of this uses internal classes and methods that are not bound to compatibility restrictions across minor and bugfix releases.
SessionImplementor si = entityManager.unwrap( SessionImplementor.class );
SessionImplementor si = (SessionImplementor) session;
Once you have the SessionImplementor
, we need to access some internal objects:
// Get the envers service, this is the central "holder" for envers in 5.x
EnversService enversService = si.getSessionFactory()
.getServiceRegistry()
.getService( EnversService.class );
// Next we get the audit entity configuration and get the audited entity name
AuditEntitiesConfiguration aec = enversService.getAuditEntitiesConfiguration();
String auditEntityName = aec.getAuditEntityName( yourEntityName );
// Now we need to get the entity information from ORM
EntityPersister persister = si.getSessionFactory().getEntityPersister( auditEntityName )
String tableName = ( (Queryable) persister ).getTableName();