title: Spring boot Hibernate data source exception null exception on application start up
body: <p>Getting</p>
  <blockquote>
  <p>ERROR [SpringApplication] Application startup failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEntityManagerFactory' defined in class path resource [com/employees/service/config/DBConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NullPointerException
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean</p>
  <p>Caused by: java.lang.NullPointerException     at
  org.hibernate.boot.model.source.internal.hbm.ModelBinder$AbstractPluralAttributeSecondPass.bindCollectionElement(ModelBinder.java:3554)
  ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]     at
  org.hibernate.boot.model.source.internal.hbm.ModelBinder$AbstractPluralAttributeSecondPass.doSecondPass(ModelBinder.java:3133)
  ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]     at
  org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1696)
  ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]     at
  org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1664)
  ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]     at
  org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287)
  ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]     at
  org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904)
  ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]     at
  org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
  ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]     at
  org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)</p>
  </blockquote>
  <p>for code</p>
  <pre><code>@ConfigurationProperties(prefix = &quot;spring.datasource.employees&quot;)
  @Bean
  public DataSource myDataSource() {
      return DataSourceBuilder.create().build();
  }
  
  @Bean
  public LocalContainerEntityManagerFactoryBean myEntityManagerFactory() {
      LocalContainerEntityManagerFactoryBean employees = createEntityManagerFactory(
          myDataSource(), &quot;Employees&quot;,
          MyMapping.MAPPINGS);
      return employees;
  }
  
  @Bean
  public EntityManager myEntityManager(@Qualifier(&quot;myEntityManagerFactory&quot;)
      EntityManagerFactory emf) {
      return entityManager(emf);
  }
  
  @Bean
  public JpaTransactionManager myTransactionManager(@Qualifier(&quot;myEntityManagerFactory&quot;)
      EntityManagerFactory emf) {
      return transactionManager(emf);
  }
  
  @Bean
  public TransactionHelper myTransactionHelper(
      @Qualifier(&quot;myTransactionManager&quot;)
          JpaTransactionManager transactionManager) {
      TransactionHelper transactionHelper = new TransactionHelper();
      transactionHelper.setPlatformTransactionManager(transactionManager);
      return transactionHelper;
  }
  
  // Common methods
  private LocalContainerEntityManagerFactoryBean createEntityManagerFactory(
      DataSource dataSource, String persistenceUnitName, String[] mappings) {
      MyLocalContainerEntityManagerFactoryBean entityManagerFactory
          = new MyLocalContainerEntityManagerFactoryBean();
      entityManagerFactory.setDataSource(dataSource);
      entityManagerFactory.setPersistenceUnitName(persistenceUnitName);
      entityManagerFactory.setPackagesToScan(&quot;com.employees.domain&quot;);
      JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
      entityManagerFactory.setJpaProperties(additionalProperties());
  
      if (mappings != null) {
          entityManagerFactory.setMappingResources(mappings);
      }
      return entityManagerFactory;
  }
  
  private JpaTransactionManager transactionManager(EntityManagerFactory emf) {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(emf);
      return transactionManager;
  }
  
  private EntityManager entityManager(EntityManagerFactory emf) {
      return SharedEntityManagerCreator.createSharedEntityManager(emf);
  }
  
  @Bean
  @Primary
  public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
      return new PersistenceExceptionTranslationPostProcessor();
  }
  
  private Properties additionalProperties() {
      Properties properties = new Properties();
      properties.setProperty(&quot;hibernate.hbm2ddl.auto&quot;, &quot;none&quot;);
      //        properties.setProperty(&quot;hibernate.show_sql&quot;, &quot;true&quot;);
      properties.setProperty(&quot;hibernate.implicit_naming_strategy&quot;,
          &quot;com.employees.domain.hibernate.BackwardsCompatibleImplicitNamingStrategy&quot;);
      properties.setProperty(&quot;hibernate.dialect&quot;, &quot;org.hibernate.dialect.MySQLInnoDBDialect&quot;);
      return properties;
  }
  
  @Bean public TraceProperties traceProperties() {
      TraceProperties traceProperties = new TraceProperties();
      Set&lt;TraceProperties.Include&gt; includes = new HashSet&lt;&gt;();
      includes.addAll(Arrays.asList(TraceProperties.Include.values()));
      traceProperties.setInclude(includes);
      return traceProperties;
  }
  </code></pre>