Spring Boot + Spring Data Envers giving error on entityManagerFactory bean not initialised

1.1k views Asked by At

I want to audit some tables in my project using Spring-Data-Envers but I am getting some errors. I have 2 spring boot projects,Claims and InsureConnect. InsureConnect is a dependency in Claims.

Claims has Spring Data Envers dependency

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-envers</artifactId>
    <version>2.6.4</version>
</dependency>

JPA configuration in Project Claims is below. I have JPA repositories in Claims project

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "claimsTransEntityManagerFactory",
        transactionManagerRef = "claimsTransTransactionManager", basePackages={"com.metamorphosys.claims.jpa.transaction"})
public class ClaimsTransactionConfig {

    private static final Logger LOGGER =  LoggerFactory.getLogger(ClaimsTransactionConfig.class);
    
    @Bean
    PlatformTransactionManager claimsTransTransactionManager() {
        return new JpaTransactionManager(claimsTransEntityManagerFactory().getObject());
    }

    @Bean
    LocalContainerEntityManagerFactoryBean claimsTransEntityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    //code...
    }

ClaimsApplication.java

@SpringBootApplication(scanBasePackages ={"com.metamorphosys"})
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
public class ClaimsApplication{
//code
}

For InsureConnect project below is the transactionConfig.java.

@Configuration
public class TransactionConfig {

    private static final Logger LOGGER =  LoggerFactory.getLogger(TransactionConfig.class);
    
    @Bean
    @Primary
    PlatformTransactionManager transTransactionManager() {
        return new JpaTransactionManager(transEntityManagerFactory().getObject());
    }

    @Bean
    @Primary
    LocalContainerEntityManagerFactoryBean transEntityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        // code

}

InsureConnectApplication.java

@SpringBootApplication
@EnableAsync
@ImportResource({ "classpath*:config/*.xml" })
@EnableJpaRepositories(entityManagerFactoryRef = "transEntityManagerFactory",
transactionManagerRef = "transTransactionManager", basePackages={"com.metamorphosys.insureconnect.jpa.transaction"}
,repositoryFactoryBeanClass = BaseRepositoryFactoryBean.class)
public class InsureConnectApplication implements ApplicationContextAware {

    private static final Logger log = LoggerFactory.getLogger(InsureConnectApplication.class);
}

Insureconnect doesn't have Spring-Data-Envers dependency

Now when I run Claims project I get below error for TransactionConfig in insureConnect project.

Cannot create inner bean '(inner bean)#64ba4522' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#64ba4522': Cannot resolve reference to bean 'transEntityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transEntityManagerFactory' defined in class path resource [com/metamorphosys/insureconnect/dataobjects/transaction/TransactionConfig.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile

the project runs fine if I remove Spring-Data-Envers dependency.

Can anyone please help me understand why the error is occurring while using Envers? Thanks in advance.

Note- I am using 2.6.4 v of Spring-Data-envers.

1

There are 1 answers

0
Jan Pieter Zuidema On

I had a similar issue with my application. I ended up adding jpa starter to my pom, afterwards my application starting working:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

I think this starter loads some extra's that spring-data-envers does not have/ or loads.