17

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
Sudhirkd
  • 233
  • 1
  • 3
  • 10
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Nathan Tuggy May 16 '15 at 02:13
  • @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. – Sudhirkd May 18 '15 at 18:53

4 Answers4

16

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);
    }
}
dahuda
  • 13
  • 4
wallance
  • 325
  • 1
  • 10
  • you 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 – Yasitha Waduge Nov 19 '15 at 07:47
4

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.

Vishal
  • 381
  • 3
  • 10
  • 1
    Trying to map Map object to jsonb with Postgres here, this answer absolutely saved my day, thanks. – m4rtin Feb 13 '18 at 12:15
0

Many thanks to Vishal for the idea. In my case it was generic-less implementation of javax.persistence.AttributeConverter that provoked this exception. Changing class MapConverter implements AttributeConverter<Map, String> { ... } to class MapConverter implements AttributeConverter<Map<String, Object>, String> { ... } really helped.

IhorP
  • 1
0

I faced the mentioned issue with mapping in OnetoOne relationship..

My app had the following OneToOne relationship.

A ||---|| B

B ||---|| C
Since its OnetoOne, I kept entity B primary key same as entity A, using @MapsId as mentioned here 

https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/

Similarly I kept entity C primary key same as entity B.

I fixed this by changing the relationship like this

A ||---|| B

A ||---|| C
abitcode
  • 1,420
  • 17
  • 24