Spring Hibernate Hibernate不会使用“ spring.jpa.hibernate.ddl-auto”生成表

时间:2019-08-15 17:39:38

标签: hibernate spring-boot create-table

我有一个带有hibernate的spring boot应用程序。
我希望休眠创建我的表并升级它们。

因此在创建sessionFactory时,我会添加以下配置:

 Properties properties = new Properties();
        properties.setProperty("spring.jpa.generate-ddl", "true");
        properties.setProperty("spring.jpa.hibernate.ddl-auto", "update");
        properties.setProperty("spring.jpa.properties.hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect");
        sessionBuilder.addProperties(properties);

,但未创建。 这是完整的代码:

@SpringBootApplication
@ComponentScan({"com.app","com.app.server","com.backend","com.backend.web"})
@EntityScan(basePackages = {"com.app.data.entity","com.backend.entity"})//todo do we need this????
@EnableAutoConfiguration
public class AppServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AppServerApplication.class, args);
    }
}


@Configuration
public class BeanConfig {


    @Value("${app.datasource.driverClassName}") String driverClassName;
    @Value("${app.datasource.url}") String url;
    @Value("${app.datasource.username}") String username;
    @Value("${app.datasource.password}") String password;

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        DataSource dataSource = DataSourceBuilder
                .create()
                .username(username)
                .password(password)
                .url(url)
                .driverClassName(driverClassName)
                .build();
        return dataSource;
    }
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(DataSource dataSource) {
        LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        Properties properties = new Properties();
        properties.setProperty("spring.jpa.generate-ddl", "true");
        properties.setProperty("spring.jpa.hibernate.ddl-auto", "update");
        properties.setProperty("spring.jpa.properties.hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect");
        sessionBuilder.addProperties(properties);
        sessionBuilder.scanPackages("com.app.data.entity","com.backend.entity");

        return sessionBuilder.buildSessionFactory();
    }
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager(
            SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(
                sessionFactory);
        return transactionManager;
    }
    @Bean
    public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
        final DataSourceInitializer initializer = new DataSourceInitializer();
        initializer.setDataSource(dataSource);
        return initializer;
    }
}

0 个答案:

没有答案