我想审核实体中的所有属性,但仅限于某些属性已更改。 按照文档(here)中描述的步骤,我创建了自己的EnversIntegrator实现:
public class EnversIntegrator extends org.hibernate.envers.event.spi.EnversIntegrator {
private static final Logger log = LoggerFactory.getLogger(EnversIntegrator.class);
private AuditConfiguration enversConfiguration;
@Override
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
log.debug(".... integrate");
final EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE);
enversConfiguration = AuditConfiguration.getFor(configuration, serviceRegistry.getService(ClassLoaderService.class));
if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
log.debug(".... Register listeners");
listenerRegistry.prependListeners(EventType.POST_DELETE, new MyPostDeleteEventListener(enversConfiguration));
listenerRegistry.prependListeners(EventType.POST_INSERT, new MyPostInsertEnversEventListener(enversConfiguration));
listenerRegistry.prependListeners(EventType.POST_UPDATE, new MyPostUpdateEnversEventListener(enversConfiguration));
}
}
@Override
public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
if (enversConfiguration != null) {
enversConfiguration.destroy();
}
}
其中:
MyPostInsertEnversEventListener extends org.hibernate.envers.event.spi.EnversPostInsertEventListenerImpl
和
MyPostUpdateEnversEventListener extends org.hibernate.envers.event.spi.EnversPostUpdateEventListenerImpl
覆盖onPostUpdate或onPostInsert(PostInsertEvent evt)无效,每次执行测试时,都会在_AUD表中添加新记录。
public void onPostUpdate(PostUpdateEvent evt) {
LOG.debug(" ... onPostUpdate " + evt.getEntity().getClass());
//here is my logic who decide if the super.onPostUpdate(evt) must be call or not
//But NO super.onPostUpdate(evt); is called !!!!!
}
更新地址实体booth onPostUpdate和onPostInsert:
[DEBUG] xxxx.hibernate.MyPostUpdateEnversEventListener - ... onPostUpdate class xxxxxxx.model.Address
[DEBUG] xxxx.hibernate.MyPostInsertEnversEventListener - ... onPostInsert class org.hibernate.envers.DefaultRevisionEntity
[DEBUG] xxxx.hibernate.MyPostInsertEnversEventListener - ... onPostInsert class java.util.HashMap
如何在_aud表中阻止创建新记录?
非常感谢!
答案 0 :(得分:0)
您需要子类化事件侦听器,例如PostInsertEnversEventListener
,在那里进行条件审核检查,如果检查通过,则调用super方法。
答案 1 :(得分:0)
调试代码我已经意识到"整合" org.hibernate.envers.event.spi.EnversIntegrator中的方法也被调用。事实上,我错放了" hibernate.listeners.envers.autoRegister "弹簧配置中的参数。这是我的工作配置:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myTestDataSource" />
<property name="packagesToScan" value="xyz.**.model" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.listeners.envers.autoRegister">false</prop>
</props>
</property>
<property name="persistenceXmlLocation"
value="classpath:spring/app-persistence.xml" />
</bean>
亚当四,你的伟大工作。
答案 2 :(得分:0)
对于较新版本,该媒体资源为 hibernate.envers.autoRegisterListeners
,而不是hibernate.listeners.envers.autoRegister
。
最后一个只是遗留的。您可以检查EnversIntegrator
和EnversServiceImpl
类。
对于春季靴子,你可以设置:
spring.jpa.properties.hibernate.envers.autoRegisterListeners=false