1

I am getting above error

Getting  java.lang.NullPointerException: Cannot invoke "org.hibernate.boot.spi.MetadataImplementor.getEntityBindings()" because "this.metadata" is null

when I am configuring JPA EntityManagerFactory pro-grammatically using Spring JPA in a Spring MVC project. This is being thrown when I am booting the Tomcat.

Here is the specific code related to

@Configuration


@Bean
public DataSource dataSource() {
    try {
        
         SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
         Class<? extends Driver> driver = (Class<? extends Driver>) Class.forName("org.h2.Driver");
         dataSource.setDriverClass(driver);
         dataSource.setUrl("jdbc:h2:mem:test");
         dataSource.setUsername("SA");
         dataSource.setPassword("");
         return dataSource;
    } catch (Exception e) {
        System.out.println( e );
        logger.error("Embedded DataSource bean cannot be created!", e);
        return null;
    }
}

@Bean
public Properties hibernateProperties() {
    Properties hibernateProp = new Properties();
    hibernateProp.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    hibernateProp.put("hibernate.hbm2ddl.auto", "create-drop");
    hibernateProp.put("hibernate.show_sql", true);
    hibernateProp.put("hibernate.max_fetch_depth", 3);
    hibernateProp.put("hibernate.jdbc.batch_size", 10);
    hibernateProp.put("hibernate.jdbc.fetch_size", 50);
    return hibernateProp;
}

@Bean
public PlatformTransactionManager transactionManager() {
    return new JpaTransactionManager(entityManagerFactory());
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);
    return vendorAdapter;
}


@Bean
public EntityManagerFactory entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setPackagesToScan("com.apress.prospring5.ch16.entities");
    factoryBean.setDataSource(dataSource());
    factoryBean.setJpaProperties(hibernateProperties());
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
    factoryBean.afterPropertiesSet();
    return factoryBean.getNativeEntityManagerFactory();
}


}

Tried various configuration with DataSource.

1

1 Answer 1

2

Update dependency version:

implementation 'org.hibernate.orm:hibernate-core:6.3.1.Final'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.