0

I'm new to JPA and am working through examples on a UDemy course and got confused with the various Spring initialization on startup options. My entity is not being created on application startup, though as an H2 in-memory database it should because it's not there.

My application-h2.properties:

spring.datasource.url=jdbc:h2:mem:ordersdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE
spring.datasource.username=ordersadmin
spring.datasource.password=secret
spring.jpa.database=mysql
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
#spring.datasource.defer-datasource-initialization=FALSE
#spring.jpa.defer-datasource-initialization=false
spring.sql.init.mode=NEVER
spring.flyway.user=ordersadmin
spring.flyway.password=secret
spring.flyway.locations=db/migration,db/specific/h2
spring.flyway.defaultSchema=orders

My application.properties:

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type.descriptor.sql=trace
spring.h2.console.enabled=true

My entity class:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.Builder;
import lombok.Data;

@Entity
@Builder
@Data
public class OrderHeader {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;
    private String customerName;
}

Ultimately, I'm trying to get the SQL for the table put into a Flyway configuration file, but prefer to start with JPA's generated SQL statement for this. I thought telling it

spring.jpa.hibernate.ddl-auto=create-drop would tell it to create or drop any tables it needed to, but it's not doing that.

My confusion comes from properties like:

spring.sql.init.mode
spring.datasource.defer-datasource-initialization

I believe these were related to non-Flyway/non-Liquibase type initialization driven by some schema.sql file, but can't find consistent docs on that.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Woodsman
  • 901
  • 21
  • 61

0 Answers0