[英]How to ensure Spring Data JPA does not do DDLs (without Spring-Boot)?
I have the following database 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;
}
}
Package com.mycompany.databaseutilities.model
contains classes, annotated by @Entity
包com.mycompany.databaseutilities.model
包含由@Entity
注释的类
Can I be sure, that it won't execute any DDL statements? 我可以确定它不会执行任何DDL语句吗? I don't wish to damage existing database. 我不想破坏现有的数据库。
You can either specify the spring.jpa.hibernate.ddl-auto
property in your spring boot application.properties
or application.yaml
file. 您可以在Spring Boot的application.properties
或application.yaml
文件中指定spring.jpa.hibernate.ddl-auto
属性。
You can also specify this at the time of creating your LocalContainerEntityManagerFactoryBean
by supplying properties: 您还可以在创建LocalContainerEntityManagerFactoryBean
通过提供属性来指定它:
final Properties jpaProperties = new Properties();
jpaProperties.put( AvailableSettings.HBM2DDL_AUTO, "false" );
entityManagerFactoryBean.setJpaProperties( jpaProperties );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.