Spring Boot H2:始终尝试使用文件数据库创建表

4uqofj5v  于 2022-11-05  发布在  Spring
关注(0)|答案(1)|浏览(254)

我已经像这样配置了application-local.yml,以便在本地进行一些测试:

spring:
      datasource:
        url: jdbc:h2:file:./data/db-test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
        username: dbtest
        password: dbtest
        driver-class-name: org.h2.Driver
      jpa:
        show-sql: true
        generate-ddl: false
        properties:
          # to prevent this issue : https://stackoverflow.com/questions/4588755/disabling-contextual-lob-creation-as-createclob-method-threw-error
          hibernate.temp.use_jdbc_metadata_defaults : false
          hibernate:
            dialect: org.hibernate.dialect.H2Dialect
        hibernate:
          ddl-auto: update
      h2:
        console:
          enabled: true
          path: /public/h2-console
      liquibase:
        enabled: false

第一次启动服务器时,我看到Hibernate中的一些日志以创建表,并创建了数据库的文件。问题是,当我重新启动服务器时,我遇到一些错误,因为它尝试再次创建相同的表、约束......我将ddl-auto设置为更新,因此我不明白为什么。我尝试将generate-ddl值更新为false,但它是相同的。
下面是我所拥有的一个例子:

Hibernate: create table myTable (id bigint not null, ..., primary key (id))
15:59:49.252 [restartedMain] WARN  o.h.t.s.i.ExceptionHandlerLoggedImpl - GenerationTarget encountered exception accepting command : Error executing DDL "create table myTable (id bigint not null, ..., primary key (id))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table myTable(id bigint not null, ..., primary key (id))" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:562)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:507)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:318)

我哪里做错了?

t2a7ltrp

t2a7ltrp1#

spring.jpa.generate-ddl: true正在强制spring data JPA重新创建表。只需将其设置为false或删除该键,因为默认值为false。这应该可以修复问题。Docs

相关问题