I am using mybatis for persisting my entities. When lazy-loading entity collections/associations is enabled, mybatis proxies the corresponding entities.
When saving back those entities, I want to use javers to determine exactly what has changed. This does not seem to play with javers.
Does javers provide an unproxy hook so that it can be used out of the box with mybatis?
The alternative would be to "unproxy" the mybatis classes manually.
Sample code:
/**
* @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);
}
}
Edit: Here is a related (unanswered) question regarding "unproxying" mybatis objects: How to convert Mybatis Javassist proxies object to source object(unproxy object)?
You can implement an ObjectAccessHook
and register it using JaversBuilder.withObjectAccessHook()
.
see how it's done for Hibernate https://javers.org/documentation/spring-integration/#hibernate-unproxy-hook