How to retrieve the audited revision of relations?

我是研究僧i 提交于 2019-11-29 12:05:02
Eric Taix

I tried to implement the second solution:

  1. First my integrator which adds a new post update listener (RevisionOnCollectionPostUpdateEventListenerImpl)

    public class RevisionOnCollectionUpdateIntegrator implements Integrator {
    private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, RevisionOnCollectionUpdateIntegrator.class.getName());
    
    public static final String REGISTER_ON_UPDATE = "org.hibernate.envers.revision_on_collection_update";
    
    @Override
    public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    
        final boolean autoRegister = ConfigurationHelper.getBoolean(REGISTER_ON_UPDATE, configuration.getProperties(), true);
        if (!autoRegister) {
            LOG.debug("Skipping 'revision_on_collection_update' listener auto registration");
            return;
        }
    
        EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
        listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE);
    
        final AuditConfiguration enversConfiguration = AuditConfiguration.getFor(configuration, serviceRegistry.getService(ClassLoaderService.class));
        if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
            listenerRegistry.appendListeners(EventType.POST_UPDATE, new RevisionOnCollectionPostUpdateEventListenerImpl(enversConfiguration));
        }
    }
    
  2. And then the post update listener (which extends):

    public class RevisionOnCollectionPostUpdateEventListenerImpl extends EnversPostUpdateEventListenerImpl {
    protected final void generateBidirectionalWorkUnits(AuditProcess auditProcess, EntityPersister entityPersister, String entityName, Object[] newState,
            Object[] oldState, SessionImplementor session) {
        // Checking if this is enabled in configuration ...
        if (!getAuditConfiguration().getGlobalCfg().isGenerateRevisionsForCollections()) {
            return;
        }
    
        // Checks every property of the entity, if it is an "owned" to-one relation to another entity.
        // If the value of that property changed, and the relation is bi-directional, a new revision
        // for the related entity is generated.
        String[] propertyNames = entityPersister.getPropertyNames();
    
        for (int i = 0; i < propertyNames.length; i++) {
            String propertyName = propertyNames[i];
            RelationDescription relDesc = getAuditConfiguration().getEntCfg().getRelationDescription(entityName, propertyName);
            if (relDesc != null && relDesc.isBidirectional() && relDesc.getRelationType() == RelationType.TO_ONE && relDesc.isInsertable()) {
                // Checking for changes
                Object oldValue = oldState == null ? null : oldState[i];
                Object newValue = newState == null ? null : newState[i];
    
                        // Here is the magic part !!!!!!!!!
                        // The super class verify if old and new value (of the owner value) are equals or not
                        // If different (add or delete) then an audit entry is also added for the owned entity
                        // When commented, an audit row for the owned entity is added when a related entity is updated
            //  if (!Tools.entitiesEqual(session, relDesc.getToEntityName(), oldValue, newValue)) {
                    // We have to generate changes both in the old collection (size decreses) and new collection
                    // (size increases).
                    if (newValue != null) {
                        addCollectionChangeWorkUnit(auditProcess, session, entityName, relDesc, newValue);
                    }
    
                    if (oldValue != null) {
                        addCollectionChangeWorkUnit(auditProcess, session, entityName, relDesc, oldValue);
                    }
            //  }
            }
        }
    }
    

It seems to work but I have to test a little bit more.

I have been unable to make the custom post update listener solution work. addCollectionChangeWorkUnit doesn't seem to exist until hibernate 4.1 where it is marked private. EnversPostUpdateEventListenerImpl seems to appear at some point in hibernate 4.0

I solved my problem by adding a hidden lastUpdated date field on my equivalent of your A entity.

@Entity
public class A {
    private Date lastModified;
    @OneToMany(mappedBy = "a", cascade = CascadeType.ALL )
    private List<B> blist;
    public void touch(){
        lastModified=new Date();
    }
}

In the related entities (like you B field), I added the following :

public class B {
    @ManyToOne
    private A a; 

    @PreUpdate
    public void ensureParentUpdated(){
        if(a!=null){
            a.touch();
        }
    }
}

This ensures that a revision is added to A whenever a revision is added to B even though it requires custom code in many entities.

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