我有一个带有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;
}
}