0

I am trying to dump data from H2 database to data.sql file in my project by executing such command:

SCRIPT TO '/home/mat/Projects/myapp/server/src/main/resources/data.sql'

Data is dumped and sql file is populated but when I am restarting app exception is thrown:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "ADDRESS" already exists; SQL statement:
CREATE MEMORY TABLE "PUBLIC"."ADDRESS"( "ID" BIGINT DEFAULT (NEXT VALUE FOR "PUBLIC"."SYSTEM_SEQUENCE_D07A8597_940F_46CC_9CCA_78B64543126F") NOT NULL NULL_TO_DEFAULT SEQUENCE "PUBLIC"."SYSTEM_SEQUENCE_D07A8597_940F_46CC_9CCA_78B64543126F", "CITY" VARCHAR(255), "COUNTRY" VARCHAR(255), "HOME_NO" VARCHAR(255), "POSTAL_CODE" VARCHAR(255), "STREET" VARCHAR(255) ) [42101-199]

Why is it? Database and its data should be initialized on startup but looks like hmm? Entites from project are generated first and that is why exception is thrown?

My app.properties file:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

spring.jpa.show-sql=true
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
Mateusz Gebroski
  • 1,274
  • 3
  • 26
  • 57

1 Answers1

2

When you shutdown your application and restart it.

Your script is executed again and it tries to create a new table hence throwing table exists error.

Therefore you need to specifiy what happens when your application is restarted.

spring.jpa.hibernate.ddl-auto=update

if you need to update your table

to delete and recreate table use

spring.jpa.hibernate.ddl-auto=create-drop

You can read more on the following links

How hibernate works

how hibernate works baeldung

malvern dongeni
  • 647
  • 4
  • 10