1

I'm using Spring MVC with Hibernate. I configure Hibernate using configuration Java class:

DbConfig.java:

@Configuration
 @EnableTransactionManagement
@EnableJpaRepositories(basePackages = "testproject", entityManagerFactoryRef = "entityManagerFactory")
 public class DbConfig {
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(getDatasource());
        entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);

        entityManagerFactoryBean.setJpaProperties(getHibernateProperties());

        entityManagerFactoryBean.setPackagesToScan("testproject");

        return entityManagerFactoryBean;
    }


    @Bean
    public DataSource getDatasource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/testproject");
        dataSource.setUsername("postgres");
        dataSource.setPassword("mypass");
        return dataSource;
    }

    @Bean
    public SessionFactory getSessionFactory() throws IOException {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setPackagesToScan("testproject");
        //getHibernateProperties method is a private method

        sessionFactoryBean.setHibernateProperties(getHibernateProperties());
        sessionFactoryBean.setDataSource(getDatasource());
        sessionFactoryBean.afterPropertiesSet();

        return sessionFactoryBean.getObject();
    }

    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager() throws IOException {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory());
        return transactionManager;
    }


    private static Properties getHibernateProperties() {
        Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        hibernateProperties.put("hibernate.show_sql", false);
        hibernateProperties.put("spring.jpa.hibernate.ddl-auto", "create");

        System.out.println();
        // other properties
        return hibernateProperties;
    }
}

The auto-creation of tables is configured inside getHibernateProperties method:

hibernateProperties.put("spring.jpa.hibernate.ddl-auto", "create");

However it doesn't seem to work: after I deploy server and try to insert an entity I get an error and the database has no tables.

3
  • What is the error? Commented Jun 26, 2020 at 22:59
  • @MinarMahmund org.hibernate.exception.SQLGrammarException: could not extract ResultSet - it's not the point here - that error happens because the tables have not been created. The question is about how to get Hibernate to auto-create tables Commented Jun 26, 2020 at 23:00
  • What does your org.hibernate.SQL logger log? Commented Jun 26, 2020 at 23:07

1 Answer 1

1

The property spring.jpa.hibernate.ddl-auto is of type org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.Hibernate which is from Spring Boot. But you are using Spring MVC.

use hibernate.hbm2ddl.auto instead.


Further Reading

This is the properties code that works:

   @Bean
    private static Properties getHibernateProperties() {
        Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        hibernateProperties.put("hibernate.show_sql", true);
        hibernateProperties.put( "hibernate.hbm2ddl.auto", "create-drop");
        return hibernateProperties;
    }
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.