简体   繁体   中英

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

Can I be sure, that it won't execute any DDL statements? 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.

You can also specify this at the time of creating your LocalContainerEntityManagerFactoryBean by supplying properties:

final Properties jpaProperties = new Properties();
jpaProperties.put( AvailableSettings.HBM2DDL_AUTO, "false" );

entityManagerFactoryBean.setJpaProperties( jpaProperties );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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