Spring Boot : Error creating bean with name 'jpaMappingContext': java.lang.NullPointerException
Solution 1
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);
}
}
Solution 2
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.
Related videos on Youtube
Sudhirkd
Updated on July 13, 2020Comments
-
Sudhirkd almost 4 years
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
-
Nathan Tuggy about 9 yearspossible duplicate of What is a Null Pointer Exception, and how do I fix it?
-
Sudhirkd almost 9 years@NathanTuggy :) I know what NullPointerException is. What could be null in this combination of frameworks I am using ? Any configuration I am missing ? Could there be a problem in entities.
-
-
Yasitha Waduge over 8 yearsyou made my day.. eventhough this was not actual problem of mine, with this answer i was able to exclude all required auto generation classes. :D thanks
-
m4rtin over 6 yearsTrying to map Map object to jsonb with Postgres here, this answer absolutely saved my day, thanks.