我有以下数据库config
@Configuration
@EnableJpaRepositories("com.mycompany.databaseutilities.repo")
@ImportResource("classpath:data_source.xml")
public class DataConfig {
@Autowired
DataSource dataSource;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean ans =
new LocalContainerEntityManagerFactoryBean();
ans.setDataSource(dataSource);
ans.setJpaVendorAdapter(jpaVendorAdapter());
ans.setPackagesToScan("com.mycompany.databaseutilities.model");
return ans;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter ans = new HibernateJpaVendorAdapter();
ans.setShowSql(false);
ans.setGenerateDdl(false); // is this sufficient?
ans.setDatabase(Database.MYSQL);
return ans;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager ans = new JpaTransactionManager();
ans.setEntityManagerFactory(entityManagerFactory().getObject());
return ans;
}
}
软件包com.mycompany.databaseutilities.model
包含类,由@Entity
我可以确定,它不会执行任何DDL语句吗?我不希望损坏现有数据库。
您可以在Spring Boot application.properties
或application.yaml
文件中指定spring.jpa.hibernate.ddl-auto
属性。
您还可以通过提供属性来创建LocalContainerEntityManagerFactoryBean
时指定此信息:
final Properties jpaProperties = new Properties();
jpaProperties.put( AvailableSettings.HBM2DDL_AUTO, "false" );
entityManagerFactoryBean.setJpaProperties( jpaProperties );