2017-03-10 42 views
0

我有以下的数据库配置如何确保Spring JPA不会执行DDL(没有Spring-Boot)?

@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语句?我不希望损害现有的数据库。

+1

spring.jpa.hibernate.ddl-AUTO =更新 –

+1

不要给DDL权限的应用程序的数据库用户/架构,你很安全。 –

+0

@KumaresanPerumal在哪里写这个?我没有任何配置文件。 – Dims

回答

0

您可以在弹簧引导application.propertiesapplication.yaml文件中指定spring.jpa.hibernate.ddl-auto属性。

您还可以通过提供的属性创建LocalContainerEntityManagerFactoryBean时注明此:

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

entityManagerFactoryBean.setJpaProperties(jpaProperties);