使用带有 mybatis 增强的 javers 类

Using javers with mybatis-enhanced classes

我正在使用 mybatis 来持久化我的实体。当启用延迟加载实体 collections/associations 时,mybatis 将代理相应的实体。

当保存回那些实体时,我想使用 javers 来确定到底发生了什么变化。这似乎与 javers 无关。

javers 是否提供了一个 unproxy hook,以便它可以与 mybatis 开箱即用?

另一种方法是手动 "unproxy" mybatis 类。

示例代码:

    /**
     * @param t The entity to be created or updated by this repository
     * @return The new entity state after it was created or updated
     */
    @Override
    @Transactional
    public T save(T entity) {
        try {
            int affectedRows;
            T oldEntity = mapper.selectById(this.getId(entity));
            if (oldEntity != null) {
                Diff diff = 
                       this.getEntityComparator().compare(oldEntity,entity);
                // publish here changes to event sourcing                    
                affectedRows = mapper.update(entity);
                if (affectedRows != 1) throw new RepositoryException();
            } else {
                affectedRows = mapper.create(entity);
                if (affectedRows != 1) throw new RepositoryException();
            }
            return mapper.selectById(this.getId(entity));
        } catch (DataAccessException ex) {
            throw this.getRepositoryException(ex);
        }
    }

编辑:这是一个关于 "unproxying" mybatis 对象的相关(未回答)问题:How to convert Mybatis Javassist proxies object to source object(unproxy object)?

您可以实施 ObjectAccessHook 并使用 JaversBuilder.withObjectAccessHook() 注册它。

看看它是如何为 Hibernate 做的 https://javers.org/documentation/spring-integration/#hibernate-unproxy-hook