Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-boot populate H2 database with schema.sql and data.sql

I set up Spring-boot to work with H2 in-memory database application.properties file is in the /config directory and it looks like, this file is processed

spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.path=/myconsole
spring.h2.console.enabled=true
spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=data.sql

This file is processed and the console appears at /myconsole But the schema.sql and data.sql are not processed and db is empty. I placed schema.sql and data.sql files both under /config and /src/main/resources.

SQL language instructions are correct and I can populate the table using console input.

Another strange thing is even though i name db as

spring.datasource.url=jdbc:h2:mem:mydb

the spring console loads another database testdb

o.s.j.d.e.EmbeddedDatabaseFactory --- Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'

How to load the H2 database correctly?

like image 869
user3687431 Avatar asked Jun 13 '16 10:06

user3687431


People also ask

How will you insert data in H2 database using data in SQL?

Syntax. Following is the basic syntax of INSERT INTO statement. INSERT INTO tableName { [ ( columnName [,...] ) ] { VALUES { ( { DEFAULT | expression } [,...] ) }

Where do I put schema in spring boot SQL?

You can simply create a import. sql file in src/main/resources and Hibernate will execute it when the schema is created.

How do I create a schema in H2 database?

You could run a script, or just a statement or two: String url = "jdbc:h2:mem:test;" + "INIT=CREATE SCHEMA IF NOT EXISTS TEST" String url = "jdbc:h2:mem:test;" + "INIT=CREATE SCHEMA IF NOT EXISTS TEST\\;" + "SET SCHEMA TEST"; String url = "jdbc:h2:mem;" + "INIT=RUNSCRIPT FROM '~/create.

What is H2 database in Spring Boot?

H2 is a relational database management system written in Java. It can be embedded in Java applications or run in the client-server mode. H2 supports a sub set of the SQL standard. H2 also provides a web console to maintain the database. You need very little configuration to connect Spring Boot application with H2.

How do I load a database in Spring Boot?

For embedded databases Spring Boot loads schema.sql and data.sql automatically from classpath. For Relational Databases, have to set spring.datasource.initialization-mode=always to load schema.sql and data.sql

When was the @SQL property introduced in Spring Boot?

This property was introduced in Spring Boot 2.5.0; we need to use spring.datasource.initialization-mode if we are using previous versions of Spring Boot. 6. @Sql Spring also provides the @Sql annotation — a declarative way to initialize and populate our test schema.

How to create an embedded DataSource schema in Spring Boot?

By default, Spring Boot automatically creates the schema of an embedded DataSource. If we need to control or customize this behavior, we can use the property spring.sql.init.mode.


1 Answers

Resolved the issue.

The spring boot app requires its ownd jdbc dependency

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

NON-boot dependency, that I had, is NOT enough alone:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
</dependency>

Without the "spring-boot-starter-jdbc" dependency "spring.datasource.url" settings in file "application.properties" are not processed. That file is actually processed, but not the jdbc settings. Spring boot will create its own testdb in the memory, which destroys the data after closing the application.

like image 106
user3687431 Avatar answered Oct 07 '22 18:10

user3687431