Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot : Error creating bean with name 'jpaMappingContext': java.lang.NullPointerException

My combination of is Spring Boot + Spring Data Jpa + Multiple Databases. I am getting following NullPointer exception when starting the application. Feels like SPring Data with Boot is not able to generate JPA Metadata. I did not get any resource related to this error.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
        at com.verient.infinipay.staticcard.Application.main(Application.java:25)
        ... 6 more
Caused by: java.lang.NullPointerException
        at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.getMetamodels(JpaMetamodelMappingContextFactoryBean.java:90)
        at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:56)
        at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:26)
        at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
        ... 21 more

My Code is :

    public EntityManagerFactory apEntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(apDataSource())
                .packages(Entity1.class, Entity2.class)
                .persistenceUnit("ap-persistent-unit")
                .build()
                .getObject();
    }

    @Bean
    public EntityManagerFactory trEntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(trDataSource())
                .packages(Entity3.class, Entity4.class)
                .persistenceUnit("tr-persistent-unit")
                .build()
                .getObject();
    }

    @Bean
    JpaTransactionManager apTransactionManager(@Qualifier("apEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }

    @Bean
    JpaTransactionManager trTransactionManager(@Qualifier("trEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }

I also have following hibernate properties in application.properties.

spring.jpa.hibernate.ddl-auto: update
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.database: H2
spring.jpa.show-sql: true
like image 451
Sudhirkd Avatar asked May 16 '15 02:05

Sudhirkd


2 Answers

Spring boot have AutoConfiguration classes enabled by default for the data sources allready on classpath. You should explicitly exclude AutoConfiguration class to disable.

Example :

@EnableAutoConfiguration(exclude = {JndiConnectionFactoryAutoConfiguration.class,DataSourceAutoConfiguration.class,
                                    HibernateJpaAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
@ComponentScan
public class MyBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyBootApplication.class, args);
    }
}
like image 52
wallance Avatar answered Nov 08 '22 21:11

wallance


For me this turned out to be due to a custom implementation of org.hibernate.usertype.UserType that I was using for mapping JSON types to Java objects.

In my particular case, it was mapping to a java.util.Map and this change in Spring Data JPA was causing a regression on upgrading to that version.

The fix was to explicitly set generic types for the Map - e.g. Map<String, Object> in my case.

like image 4
Vishal Avatar answered Nov 08 '22 20:11

Vishal