Spring Boot Data JPA with H2 and data.sql - Table not Found
Asked Answered
F

4

30

I have a Spring Boot 2.5.0 project. I'm using Spring Data JPA with the H2 in-memory database. I want to populate data on startup with a data.sql file but I'm getting a table not found exception. If I remove the data.sql file, I can see that a table for my entity does get created automatically. But if I include the data.sql file, I get the error saying the table doesn't exist. Maybe it is an error with my sql syntax of I have misconfigured the H2 database?

applicaltion.yml

spring:
  datasource:
    url: jdbc:h2:mem:test
    driverClassName: org.h2.Driver
    username: sa
    password: sa
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect

debug: true  

data.sql

INSERT INTO BUSINESS_SUMMARY VALUES (1, "ALM470", "B48", 3);

BusinessSummary.java entity

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
public class BusinessSummary {

    @Id
    private Long id;
    private String businessId;
    private String businessDomainId;
    private Integer cityCode;
}

BusinessSummaryRepository.java

@Repository
public interface BusinessSummaryRepository extends JpaRepository<BusinessSummary, Long> {
}

Exception:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "BUSINESS_SUMMARY" not found; SQL statement:
INSERT INTO BUSINESS_SUMMARY VALUES(1, "ALM470", "B48", 3) [42102-200]
Fillender answered 28/5, 2021 at 19:47 Comment(0)
N
13

If you're using hibernate as a JPA implementation, the best way I think is by using the file import.sql instead of data.sql for Database Initialization.

for more information on database initialization see the official Spring Boot documentation Database Initialization

Ninny answered 8/1, 2022 at 20:44 Comment(0)
O
65
spring.jpa.defer-datasource-initialization=true

By default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase.

If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.

you'll have to convert spring.jpa.defer-datasource-initialization to yml.

Ortrude answered 28/5, 2021 at 19:49 Comment(5)
Good grief. I literally spent 2 solid weeks on this. I finally gave up in disgust and starting writing @BeforeEach code blocks on all unit tests to populate the H2 database with data because loading data.sql always resulted in 'TABLE <> NOT FOUND' errors. Hours and hours of frustration. This was the ticket: spring.jpa.defer-datasource-initialization=true Thank you so much!Caesar
this is very important to know , was stuck for daysDietsche
Was scratching my head after bumping my spring boot versionStickpin
This works perfectly fine. What also worked for me was to rename data.sql to import.sql (no idea why even without adding spring.jpa.defer-datasource-initialization)Maieutic
By default, data.sql and schema.sql both run before Hibernate. say, if you have a sequence start with 1000 in schema.sql, which is different from your default sequence start in entity class, without putting "spring.jpa.defer-datasource-initialization" in application.yml, sequence start with 1000 will be used.Nomenclature
N
13

If you're using hibernate as a JPA implementation, the best way I think is by using the file import.sql instead of data.sql for Database Initialization.

for more information on database initialization see the official Spring Boot documentation Database Initialization

Ninny answered 8/1, 2022 at 20:44 Comment(0)
E
0

in addition to defer-datasource-initialization: true, you may also need

spring:
  sql:
    init:
      mode: always
Edmondo answered 1/3, 2022 at 3:19 Comment(0)
M
0
spring.jpa.defer-datasource-initialization = true    
spring.sql.init.mode = always

if still doesn`t work try renaming the file from data.sql to import.sql

Matroclinous answered 20/3, 2022 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.