1

I have configured the application-local.yml like this to do some tests in local:

    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

The first time the server is started, I see some logs from Hibernate to create tables and the file for the database is created. The issue is that when I restart the server, I have some errors because it tries to create again the same tables, constraints... I have the ddl-auto set to update so I do not understand why. I tried to update the generate-ddl value to false but it is the same.

Here is an exemple of what I have:

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)

What am I doing wrong please?

phildeg31
  • 169
  • 1
  • 14
  • Can you try setting `hibernate.ddl-auto: none` and see if that fixes it? If not, are you sure the configuration file is loaded? – Garuno Nov 02 '22 at 15:13
  • By setting `ddl-auto` to none, then no update will be done if I update an entity right? I would like to keep the update of the H2 database if I add or update an entity. – phildeg31 Nov 03 '22 at 13:46
  • Yes, this is correct. I just wanted to make sure the configuration file is actually loaded. This is just a debugging step and you should change the value later – Garuno Nov 03 '22 at 13:55
  • I tried and with none there is no error when the server starts. – phildeg31 Nov 03 '22 at 13:59

1 Answers1

0

The spring.jpa.generate-ddl: true is forcing spring data JPA to recreate the tables. Just set this to false or delete the key as the default is false. This should fix the Problem. Docs

Garuno
  • 1,935
  • 1
  • 9
  • 20
  • Thank you for your answer but it is the same with generate-ddl set to false. I set an example of error. – phildeg31 Nov 02 '22 at 15:01