【问题标题】:How to ensure Spring Data JPA does not do DDLs (without Spring-Boot)?如何确保 Spring Data JPA 不执行 DDL(没有 Spring-Boot)?
【发布时间】:2017-03-10 14:07:51
【问题描述】:

我有以下数据库配置

@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.jpa.hibernate.ddl-auto=update
  • 不要将 DDL 权限授予应用程序的 db 用户/模式,这样你就安全了。
  • @KumaresanPerumal 在哪里写这个?我没有任何配置文件。
  • @DraganBozanovic 我需要应用程序才能进行 DDL,我只是不需要它自己来做这件事。

标签: java mysql spring hibernate spring-data-jpa


【解决方案1】:

您可以在 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 );

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2020-02-25
    • 1970-01-01
    • 2020-05-04
    • 2017-11-29
    相关资源
    最近更新 更多