简体   繁体   中英

How not to update the ddl schema in hibernate

I try to run an application with spring batch and scheduler. I see that the ddl schema is always updated and I don't want to change my ddl schema.

I try this in my application.properties file :

hibernate.hbm2ddl.auto=validate|none

But it doesn't resolve my problem.

Here are my differents files :

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

And my class where I define my datasource

@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;
  }
}

I see in the console logs like :

org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000388: Unsuccessful: alter table tarif add constraint FK_lub9g2gwhub3a7pc7u67vp3cr foreign key (forfait_id) references forfait

Can somebody help me on this error?

Set

spring.jpa.hibernate.ddl-auto=none

in application.properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM