mcpierce
mcpierce

Reputation: 321

Spring-boot application, H2 embedded database - but tables disappear when the application closes

My database.properties file is:

datasource.driver=org.h2.Driver
datasource.url=jdbc:h2:file:./test_database/comixed_db;create=true
datasource.username=sa
datasource.password=

hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.batch.size=20
hibernate.current.session.context.class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.generate_statistics=false
hibernate.enable_lazy_load_no_trans=false

When my application starts up I see I am able to see the tables by using the h2.bat tool and peeking at the database from a web browser. However, when I shut down the application and then go back to the database with the h2.bat tool the tables are all gone!

AM I doing something incorrectly with my hibernate configuration? I am not using create-drop but update since this code is currently in flux and I'd like the tables to be adjusted as changes occur. But that doesn't seem to be the issue since it's at app shutdown that the tables keep going away.

Any help would be appreciated.

Upvotes: 1

Views: 4437

Answers (2)

mcpierce
mcpierce

Reputation: 321

By adding the following line to applications.properties:

spring.jpa.hibernate.ddl-auto=update

Spring-boot stopped dropping tables when the application exits.

Upvotes: 2

Maciej Kowalski
Maciej Kowalski

Reputation: 26562

If you want spring boot to catch your hibernate properties you should prefix them with spring.jpa, so:

spring.jpa.hibernate.ddl-auto=update

Otherwise, and that is the case in my opinion, spring will use the default create-drop options as it is dealing with an H2 in-memory database.

Upvotes: 2

Related Questions