如何确保弹簧数据JPA不会执行DDLS(没有弹簧靴)



我有以下数据库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.propertiesapplication.yaml文件中指定spring.jpa.hibernate.ddl-auto属性。

您还可以通过提供属性来创建LocalContainerEntityManagerFactoryBean时指定此信息:

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

最新更新