简体   繁体   English

如何确保Spring Data JPA不执行DDL(没有Spring-Boot)?

[英]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.propertiesapplication.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM