Use both envers RevisionRepository and JpaRepository in the same application

3.6k views Asked by At

I want to have a repository for entities (regular JPA repository) as well as a separate repository that keeps track of audit information (a RevisionRepository, part of hibernate envers).

I cannot seem to get this to work in my application.

As far as I can understand, each type of repository needs to be instantiated with it's own factory (JpaRepository with repositoryFactoryBeanClass, and RevisionRepository with EnversRevisionRepositoryFactoryBean), and that can be set with the @EnableJpaRepositories annotation.

The issue is that only one of that annotation can be on my main class. I have seen an example of this being done in xml form (here), but I don't know how to do this with annotations.

How can this be done?

2

There are 2 answers

2
Naros On

The EnversRevisionRepositoryFactoryBean extends the JpaRepositoryFactoryBean so you should only have to specify the EnversRevisionRepositoryFactoryBean in your configuration to get both to work for you.

What happens internally is that if the EnversRevisionRepositoryFactoryBean determines that your repository does not implement the the correct interface, it will delegate to the super implementation, which in this case is the JpaRepositoryFactoryBean.

0
Prashant Shilimkar On

Somehow you need to create two separate configuration classes for JpaRepositoryFactoryBean and EnversRevisionRepositoryFactoryBean as show in following code.

@EnableJpaRepositories(basePackages = "com.example.jpa.dao")
class JpaConfig {}

@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean, basePackages = "com.example.envers.dao")
class EnversConfig {}

It works for me. But could not get why EnversRevisionRepositoryFactoryBean works only for RevisionRepository and not JPRepository though EnversRevisionRepositoryFactoryBean extends JpaRepositoryFactoryBean.

Some one please edit answer and provide explanation so it will be helpful for others as well.