Why is spring boot ignoring my schema.sql file?

43 views Asked by At
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

src/main/resources/schema.sql

spring.sql.init.mode=always
spring.datasource.url=jdbc:h2:file:/tmp/vuls
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=none

I've been all over SO and the correct procedure for H2 database creation apparently changes with every Spring Boot release, because there are various and sundry answers out there that aren't working for me.

2

There are 2 answers

4
Faraj Khademi On

It seems like you're encountering issues with initializing your H2 database using the schema.sql file in your Spring Boot application. Here are a few things to check:

  1. Configuration Properties: Verify that your application.properties or application.yml file contains the correct properties to specify the SQL script and other database configuration details.

To use H2 embedded:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=admin
spring.datasource.password=password
spring.sql.init.mode=embedded

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

Then to access to h2 console :

http://localhost:8080/h2-console

  1. Check Logs: Look for any error messages or warnings related to database initialization in your application logs. This can provide valuable insights into why the schema initialization is not happening as expected. Pay attention to any exceptions or warnings related to database connectivity or schema initialization.

  2. Dependencies: Double-check that your project dependencies are correctly configured. Since you're using H2 as an embedded database, ensure that the H2 dependency is included in your project's build configuration (pom.xml for Maven or build.gradle for Gradle).

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

If you continue to encounter problems, providing specific error messages or logs can help in diagnosing the issue further.

0
wytten On

As it turns out, it was more of a Batch issue than a Boot issue. What worked for me was adding the application property 'spring.batch.jdbc.initialize-schema=always'