Thursday, 24 June 2021

Init Database in Spring using Hibernate

Instructing JPA creating schema

Spring Application property spring.jpa.hibernate.ddl-auto instructs how JPA creating a database schema.

You can set spring.jpa.hibernate.ddl-auto explicitly; these values  are none, validate, update, create-drop

Database categories

Databases are categorized into embedded or real databases.

An embedded database is detected by looking at the Connection type: hsqldb, h2 and derby are embedded, the rest are considered as real databases.

Ddl-auto default value

Spring Boot chooses a default ddl-auto value  according to databae catogories. 

for an embedded database: 

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

for an real database: 

spring.jpa.hibernate.ddl-auto = none

Be careful when switching from in-memory to a “real” database that you don’t make assumptions about the existence of the tables and data in the new platform. You either have to set ddl-auto explicitly or use one of the other mechanisms to initialize the database.


when spring.jpa.hibernate.ddl-auto = create-drop 
a file named import.sql in the root of the classpath is executed on startup if Hibernate creates the schema from scratch (that is, if the ddl-auto property is set to create or create-drop).
when spring.jpa.hibernate.ddl-auto = none
schema.sql instructing creating schema from scratch.
data.sql populating data into the database. 

No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...