Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Envers: Allow to extend the default EnversRevisionRepositoryImpl #3252

Open
TMRGZ opened this issue Dec 1, 2023 · 0 comments
Open

Envers: Allow to extend the default EnversRevisionRepositoryImpl #3252

TMRGZ opened this issue Dec 1, 2023 · 0 comments
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement

Comments

@TMRGZ
Copy link

TMRGZ commented Dec 1, 2023

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 1, 2023
@mp911de mp911de added status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants