如何不在hibernate中更新ddl架构

时间:2016-06-21 13:41:55

标签: java spring hibernate schema ddl

我尝试使用spring batch和scheduler运行应用程序。我看到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 tarif添加约束FK_lub9g2gwhub3a7pc7u67vp3cr外来   key(forfait_id)引用forfait

有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:2)

设置

spring.jpa.hibernate.ddl-auto=none

在application.properties中。