在Mybatis增强类中使用javers

时间:2019-04-28 23:48:54

标签: mybatis javers

我正在使用mybatis来保留我的实体。启用延迟加载实体集合/关联时,mybatis代理相应的实体。

当保存这些实体时,我想使用javers来确定发生了什么变化。这似乎不适用于三叶草。

javer是否提供了一个无代理的挂钩,以便可以与mybatis一起使用?

替代方法是手动“取消代理” 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);
        }
    }

编辑:这是有关“取消代理” mybatis对象的相关(未回答)问题:How to convert Mybatis Javassist proxies object to source object(unproxy object)?

1 个答案:

答案 0 :(得分:0)

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

查看Hibernate的工作方式 https://javers.org/documentation/spring-integration/#hibernate-unproxy-hook