soandos
soandos

Reputation: 5146

Use both envers RevisionRepository and JpaRepository in the same application

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?

Upvotes: 0

Views: 3945

Answers (2)

Prashant Shilimkar
Prashant Shilimkar

Reputation: 8840

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.

Upvotes: 0

Naros
Naros

Reputation: 21133

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.

Upvotes: 1

Related Questions