Skip to content

Envers: Allow to extend the default EnversRevisionRepositoryImpl #3252

Open
@TMRGZ

Description

@TMRGZ

Recently in my job I had to expand the functionality of the RevisionRepository and I thought that could be great if this posibility is added to the Spring Data project.

To achieve this, I added the following method to the EnversRevisionRepositoryFactoryBean, treating it as if we were modifying the revision entity:

public void setRepositoryImplClass(Class<? extends EnversRevisionRepositoryImpl<T, S, N>> repositoryImplClass) {
    this.repositoryImplClass = repositoryImplClass;
}

This ensures the default behavior when the new parameter is null:

private static class RevisionRepositoryFactory<T, ID, N extends Number & Comparable<N>> extends JpaRepositoryFactory {

    private final Class<?> repositoryClass;

    public RevisionRepositoryFactory(EntityManager entityManager, Class<?> revisionEntityClass, Class<?> repositoryImplClass) {

        // original implementation

        this.repositoryClass = repositoryImplClass != null ? repositoryImplClass : EnversRevisionRepositoryImpl.class;
    }

    @Override
    protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) {

        Object fragmentImplementation = getTargetRepositoryViaReflection(
                repositoryClass,
                getEntityInformation(metadata.getDomainType()),
                revisionEntityInformation,
                entityManager
        );

        // original implementation
    }
}

Additionally, to maintain consistency with the return types of the original repository implementation, I changed the visibility of these methods and classes to protected:

public class EnversRevisionRepositoryImpl<T, ID, N extends Number & Comparable<N>>
        implements RevisionRepository<T, ID, N> {

    protected AuditOrder mapRevisionSort(RevisionSort revisionSort)

    protected List<AuditOrder> mapPropertySort(Sort sort)

    protected AuditQuery createBaseQuery(ID id)

    protected Revision<N, T> createRevision(QueryResult<T> queryResult)

    protected static class QueryResult<T> {
        protected QueryResult(Object[] data)
        protected RevisionMetadata<?> createRevisionMetadata()
        protected static RevisionMetadata.RevisionType convertRevisionType(RevisionType datum)
    }

}

Feel free to provide comments or advice in case I've attempted to reinvent the wheel or if there's an easier way to achieve this exact functionality

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions