Get audit table name from hibernate envers?

风格不统一 提交于 2019-12-11 15:46:00

问题


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 auditing postfix.


回答1:


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.

Using JPA

SessionImplementor si = entityManager.unwrap( SessionImplementor.class );

Using Native Hibernate

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();


来源:https://stackoverflow.com/questions/48827478/get-audit-table-name-from-hibernate-envers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!