Get audit table name from hibernate envers?

后端 未结 1 1486
北海茫月
北海茫月 2021-01-28 18:29

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

相关标签:
1条回答
  • 2021-01-28 18:37

    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();
    
    0 讨论(0)
提交回复
热议问题