5

I'm using a global custom repository in my project which extends QueryDslJpaRepository:

   public class CustomPagingAndSortingRepositoryImpl<T, ID extends Serializable> extends QueryDslJpaRepository<T, ID>
    implements CustomPagingAndSortingRepository<T, ID> {

And the interface:

   public interface CustomPagingAndSortingRepository<T, ID extends Serializable>
    extends JpaRepository<T, ID>, QueryDslPredicateExecutor<T> {

And then on my configuration I annotate it with:

@EnableJpaRepositories(repositoryBaseClass = CustomPagingAndSortingRepositoryImpl.class)

All is working fine, but now I was trying to add auditing support to my entities by using spring-data-envers and according to the docs I should use a specific repository factory bean class :

@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class, repositoryBaseClass = CustomPagingAndSortingRepositoryImpl.class)

Now obviously if I do this things won't work because my repositories will now be created through the EnversRevisionRepositoryFactoryBean class and will no longer be of CustomPagingAndSortingRepositoryImpl type.

How can I support something like this? I'm not seeing how since my custom repository need to extend from QueryDslJpaRepository already.

Naros
  • 19,928
  • 3
  • 41
  • 71
mfc
  • 313
  • 1
  • 3
  • 15
  • You might want to watch https://jira.spring.io/browse/DATACMNS-102 It's getting implemented right now, and might improve the situation. – Jens Schauder Jun 12 '17 at 13:18

1 Answers1

0

I think the relevant part for you is this method of EnversRevisionRepositoryFactoryBean:

@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
    return EnversRevisionRepositoryImpl.class;
}

Here you really want your CustomPagingAndSortingRepositoryImpl returned. So I would try the following:

  1. extend EnversRevisionRepositoryFactoryBean and overwrite getRepositoryBaseClass to return your CustomPagingAndSortingRepositoryImpl.

  2. Make CustomPagingAndSortingRepositoryImpl extend EnversRevisionRepositoryImpl.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • 1
    I'm afraid that won't work because CustomPagingAndSortingRepositoryImpl is already extending QueryDslJpaRepository so it cannot extend EnversRevisionRepositoryImpl – mfc Jun 09 '17 at 09:14
  • implementing all the interfaces implemented by both classes should be sufficient. So extend from one + the missing interface and copy the code from the other implementation (or delegate to an instance of it). – Jens Schauder Jun 09 '17 at 09:39
  • I was trying to avoid copy pasting code from the other implementations because I don't think that's a good solution (would need to review it every time I update the library to a new version). I guess delegating to a new instance would work, that's what i was trying, but was getting trouble setting the RevisionEntityInformation for the EnversRevisionRepositoryImpl constructor, I don't understand how/when revisionEntityClass is set in EnversRevisionRepositoryFactoryBean. – mfc Jun 09 '17 at 09:55