Skip to content
Advertisement

Why AuditReader can’t be Autowired in the Repository

IN my springbootapp I have the following repository:-

@Repository
public class RevisionRepository {

private AuditReader auditReader;

public RevisionRepository(AuditReader auditReader) {
    this.auditReader = auditReader;
  }
}

When I run this app. I got this error:-

Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled. 2020-12-24 21:09:15 –


APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in com.howtodoinjava.demo.repository.RevisionRepository required a bean of type ‘org.hibernate.envers.AuditReader’ that could not be found.

Action:

Consider defining a bean of type ‘org.hibernate.envers.AuditReader’ in your configuration.

How can I make this work?

Advertisement

Answer

This fixed my issue. Hopefully, it will be helpful for others:-

@Configuration
public class RevisionConfiguration {

@Autowired
private AuditReader auditReader;

private final EntityManagerFactory entityManagerFactory;

public RevisionConfiguration(EntityManagerFactory entityManagerFactory) {
    this.entityManagerFactory = entityManagerFactory;
}

@Bean
AuditReader auditReader() {
    return AuditReaderFactory.get(entityManagerFactory.createEntityManager());
  }
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement