2016-06-21 56 views
0

我嘗試使用spring批處理和調度程序運行應用程序。我看到ddl模式總是更新,我不想更改我的ddl模式。如何不更新休眠中的ddl模式

我嘗試這在我的application.properties文件:

hibernate.hbm2ddl.auto=validate|none 

但它不解決我的問題。

這裏是我的型動物文件:

application.properties

spring.datasource.url=jdbc:postgresql://localhost/ussd_service 
spring.datasource.username=root 
spring.datasource.password=password 

spring.datasource.driver-class-name=org.postgresql.Driver 
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect 

#Hibernate Configuration: 
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect 
hibernate.show_sql=true 

#DB initialization 
hibernate.hbm2ddl.auto=validate 
org.hibernate.tool.hbm2ddl=validate 
spring.jpa.hibernate.ddl-auto=validate 

而且我的課,我定義我的數據源

@Configuration 
@EnableBatchProcessing 
@EntityScan("com.package.myentity") 
@ComponentScan(basePackages = {"com.package.batch.", 
           "com.package.repository"}) 
@PropertySource("classpath:application.properties") 
public class BatchSmsJobConfig { 

    @Value("${spring.datasource.driver-class-name}") 
    private String databaseDriver; 
    @Value("${spring.datasource.url}") 
    private String databaseUrl; 
    @Value("${spring.datasource.username}") 
    private String databaseUsername; 
    @Value("${spring.datasource.password}") 
    private String databasePassword; 

    @Bean 
    public ItemReader<Souscription> reader() throws Exception { 
    java.util.Date now = new java.util.Date(); 
    java.sql.Date date = new java.sql.Date(now.getTime()); 
    String jpqlQuery = "select u from Users u"; 

    JpaPagingItemReader<Souscription> reader = new JpaPagingItemReader<Souscription>(); 
    reader.setQueryString(jpqlQuery); 
    reader.setParameterValues(Collections.<String, Object>singletonMap("date", date)); 
    reader.setEntityManagerFactory(entityManagerFactory().getObject()); 
    //reader.setPageSize(3); 
    reader.afterPropertiesSet(); 
    reader.setSaveState(true); 

    return reader; 
    } 

    @Bean 
    public SouscriptionItemProcessor processor() { 
    System.out.println("Processing!"); 
    return new SouscriptionItemProcessor(); 
    } 


    @Bean 
    public ItemWriter<Sms> writer() { 
    System.out.println("Writing info into DB!"); 
    JpaItemWriter writer = new JpaItemWriter<Sms>(); 
    writer.setEntityManagerFactory(entityManagerFactory().getObject()); 

    return writer; 
    } 

    //@Bean 
    //public JobExecutionListener listener() { 
    // return new JobCompletionNotificationListener(jdbcTemplate); 
    //} 

    @Bean 
    public Job sendSMStoSubscribersJob(JobBuilderFactory jobs, Step s1) { 

    return jobs.get("import") 
     .incrementer(new RunIdIncrementer()) 
     .flow(s1) 
     .end() 
     .build(); 
    } 

    @Bean 
    public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Souscription> reader, 
        ItemWriter<Sms> writer, SouscriptionItemProcessor processor) { 
    return stepBuilderFactory.get("step1") 
     .<Souscription, Sms>chunk(1) 
     .reader(reader) 
     .processor(processor) 
     .writer(writer) 
     .build(); 
    } 

    @Bean 
    public DataSource dataSource() { 
    DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName(databaseDriver); 
    dataSource.setUrl(databaseUrl); 
    dataSource.setUsername(databaseUsername); 
    dataSource.setPassword(databasePassword); 
    return dataSource; 
    } 

    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 

    LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); 
    lef.setPackagesToScan("com.mobilproafrica.batch.sms"); 
    lef.setDataSource(dataSource()); 
    lef.setJpaVendorAdapter(jpaVendorAdapter()); 
    lef.setJpaProperties(new Properties()); 
    return lef; 
    } 

    @Bean 
    public JpaVendorAdapter jpaVendorAdapter() { 
    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); 
    jpaVendorAdapter.setDatabase(Database.POSTGRESQL); 
    jpaVendorAdapter.setGenerateDdl(true); 
    jpaVendorAdapter.setShowSql(true); 

    jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect"); 
    return jpaVendorAdapter; 
    } 
} 

我在控制檯日誌中看到類似:

org.hibernate.tool.hbm2ddl.SchemaUpdate:HHH000388:不成功: ALTER TABLE塔裏夫添加約束FK_lub9g2gwhub3a7pc7u67vp3cr國外 鍵(forfait_id)引用forfait

有人可以幫助我這個錯誤?

+0

如果您不使用該屬性,則僅添加屬性將不起作用。如果你使用的是Spring Boot,那甚至是錯誤的屬性。 –

+0

請提供一些關於如何配置休眠的信息。你使用springboot還是hibernate.cfg.xml,或者你使用datasource定義自己的「org.springframework.orm.hibernate3.LocalSessionFactoryBean」? – gmaslowski

+0

@gmaslowski,是的,我正在使用springboot。我認爲我唯一需要配置的文件是application.properties –

回答

2

spring.jpa.hibernate.ddl-auto=none 

在application.properties。

+0

對不起,它不起作用 –

+0

好的,我找到了解決方案。我只需要在我的bean jpavendorAdapter中將常量DDL設置爲false,如下所示:jpavendorAdapter.setGenerateDdl(false) –

+0

@TheGuide這就是爲什麼我問你如何配置數據源..而你的回答說你只是配置是application.properties(注意代碼也是配置!)是誤導性的。很好,你已經知道了。 – gmaslowski