【问题标题】:How not to update the ddl schema in hibernate如何不更新hibernate中的ddl模式
【发布时间】:2016-06-21 13:41:54
【问题描述】:

我尝试使用 Spring Batch 和调度程序运行应用程序。我看到 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:不成功: 更改表 tarif 添加约束 FK_lub9g2gwhub3a7pc7u67vp3cr 外国 关键(forfait_id)引用forfait

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

【问题讨论】:

  • 如果您不使用该属性,则仅添加该属性是行不通的。如果您使用的是 Spring Boot,那甚至是错误的属性。
  • 请提供一些关于如何配置休眠的信息。你是用springboot,还是hibernate.cfg.xml,还是用datasource定义自己的“org.springframework.orm.hibernate3.LocalSessionFactoryBean”?
  • @gmaslowski,是的,我正在使用 springboot。而且我认为我必须配置的唯一文件是 application.properties

标签: java spring hibernate schema ddl


【解决方案1】:

设置

spring.jpa.hibernate.ddl-auto=none

在 application.properties 中。

【讨论】:

  • 好的,我找到了解决方案。我只需要像这样在我的 bean jpavendorAdapter 中将generte DDL 设置为 false:jpavendorAdapter.setGenerateDdl(false)
  • @TheGuide 这就是为什么我问你如何配置你的数据源。你的回答说你只是配置是 application.properties (注意代码也是配置!)是误导.很好,你已经弄清楚了。
猜你喜欢
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
  • 2010-11-23
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多