Java 如何在hibernate中不更新ddl模式

Java 如何在hibernate中不更新ddl模式,java,spring,hibernate,schema,ddl,Java,Spring,Hibernate,Schema,Ddl,我尝试使用spring批处理和调度程序运行应用程序。我看到ddl模式总是更新的,我不想更改我的ddl模式 我在application.properties文件中尝试以下操作: hibernate.hbm2ddl.auto=validate|none 但这并不能解决我的问题 以下是我的不同文件: 应用程序属性 以及我定义数据源的类 @Configuration @EnableBatchProcessing @EntityScan("com.package.myentity") @Componen

我尝试使用spring批处理和调度程序运行应用程序。我看到ddl模式总是更新的,我不想更改我的ddl模式

我在application.properties文件中尝试以下操作:

hibernate.hbm2ddl.auto=validate|none
但这并不能解决我的问题

以下是我的不同文件:

应用程序属性

以及我定义数据源的类

@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;
  }
}
@配置
@启用批处理
@EntityScan(“com.package.myentity”)
@ComponentScan(basePackages={“com.package.batch.”,
“com.package.repository”})
@PropertySource(“类路径:application.properties”)
公共类BatchsJobConfig{
@值(${spring.datasource.driver类名}”)
私有字符串数据库驱动程序;
@值(${spring.datasource.url}”)
私有字符串数据库URL;
@值(${spring.datasource.username}”)
私有字符串数据库用户名;
@值(${spring.datasource.password}”)
私有字符串数据库密码;
@豆子
public ItemReader()引发异常{
java.util.Date now=new java.util.Date();
java.sql.Date-Date=new java.sql.Date(now.getTime());
String jpqlQuery=“从用户u中选择u”;
JpaPagingItemReader=新的JpaPagingItemReader();
reader.setQueryString(jpqlQuery);
reader.setParameterValues(Collections.singletonMap(“date”,date));
setEntityManagerFactory(entityManagerFactory().getObject());
//设置页面大小(3);
reader.afterPropertiesSet();
reader.setSaveState(true);
返回读取器;
}
@豆子
public SouscriptionItemProcessor处理器(){
System.out.println(“处理!”);
返回新的SouscriptionItemProcessor();
}
@豆子
公共项目编写器(){
System.out.println(“将信息写入数据库!”);
JpaItemWriter=新的JpaItemWriter();
writer.setEntityManagerFactory(entityManagerFactory().getObject());
返回作者;
}
//@豆子
//PublicJobExecutionListener(){
//返回新的JobCompletionNotificationListener(jdbcTemplate);
//}
@豆子
公共作业SendsToSubscribers作业(JobBuilderFactory作业,步骤s1){
返回作业。获取(“导入”)
.incrementer(新的RunIdIncrementer())
.流量(s1)
(完)
.build();
}
@豆子
公共步骤step1(StepBuilderFactory StepBuilderFactory、ItemReader reader、,
ItemWriter编写器,SouscriptionItemProcessor处理器){
返回stepBuilderFactory.get(“step1”)
.chunk(1)
.读卡器(读卡器)
.处理器(处理器)
.作者(作者)
.build();
}
@豆子
公共数据源数据源(){
DriverManager数据源dataSource=新的DriverManager数据源();
setDriverClassName(databaseDriver);
setUrl(databaseUrl);
setUsername(databaseUsername);
setPassword(databasePassword);
返回数据源;
}
@豆子
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean lef=新的LocalContainerEntityManagerFactoryBean();
lef.setPackagesToScan(“com.mobileproafrica.batch.sms”);
lef.setDataSource(dataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter());
lef.setJpaProperties(新属性());
返回lef;
}
@豆子
公共JpaVendorAdapter JpaVendorAdapter(){
HibernateJpaVendorAdapter=新的HibernateJpaVendorAdapter();
setDatabase(Database.POSTGRESQL);
jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setShowSql(true);
setDatabasePlatform(“org.hibernate.dialent.postgresqldatalent”);
返回JPA适配器;
}
}
我在控制台日志中看到如下内容:

org.hibernate.tool.hbm2ddl.SchemaUpdate:hh000388:未成功: 更改表tarif添加约束FK_LUB9G2GWUB3A7PC7U67VP3CR外部 键(forfait_id)引用forfait

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

Set

spring.jpa.hibernate.ddl-auto=none
在application.properties中设置

spring.jpa.hibernate.ddl-auto=none

在application.properties中。

如果不使用某个属性,则仅添加该属性是无效的。如果您使用的是Spring Boot,那么这甚至是错误的属性。请提供一些有关如何配置hibernate的信息。您是否使用springboot,或hibernate.cfg.xml,或使用datasource定义您自己的“org.springframework.orm.hibernate3.LocalSessionFactoryBean”?@gmaslowski,是的,我使用的是springboot。我认为我必须配置的唯一文件是application.properties,如果不使用该属性,则仅添加属性是无效的。如果您使用的是Spring Boot,那么这甚至是错误的属性。请提供一些有关如何配置hibernate的信息。您是否使用springboot,或hibernate.cfg.xml,或使用datasource定义您自己的“org.springframework.orm.hibernate3.LocalSessionFactoryBean”?@gmaslowski,是的,我使用的是springboot。我想我唯一需要配置的文件是application.propertiesOK,我找到了解决方案。我只需要在我的bean jpavendorAdapter中将generte DDL设置为false,如下所示:jpavendorAdapter.setGenerateDdl(false)@TheGuide,这就是为什么我问您如何配置数据源的原因。。您的回答是application.properties(请注意,代码也是配置!)是误导性的。很好,你已经弄明白了。好的,我找到了解决办法。我只需要在我的bean jpavendorAdapter中将generte DDL设置为false,如下所示:jpavendorAdapter.setGenerateDdl(false)@TheGuide,这就是为什么我问您如何配置数据源的原因。。您的回答是application.properties(请注意,代码也是配置!)是误导性的。很好,你已经弄明白了。