English 中文(简体)
Spring Boot Data JPA with H2 and data.sql - Table not Found
原标题:

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]
最佳回答

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

问题回答
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.

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

spring:
  sql:
    init:
      mode: always
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





相关问题
insert a BLOB via a sql script?

I have an H2 database (http://www.h2database.com) and I d like to insert a file into a BLOB field via a plain simple sql script (to populate a test database for instance). I know how to do that via ...

"Create table if not exists" - how to check the schema, too?

Is there a (more or less) standard way to check not only whether a table named mytable exists, but also whether its schema is similar to what it should be? I m experimenting with H2 database, and ...

h2 (embedded mode ) database files problem

There is a h2-database file in my src directory (Java, Eclipse): h2test.db The problem: starting the h2.jar from the command line (and thus the h2 browser interface on port 8082), I have created 2 ...

Starting/stopping H2 with ant

I installed H2 on a Windows PC. I would start H2 from ant so that it can be automatically started/stopped during a test suite execution. How can I do this with ant? Have I to call .bat in ./service ...

What is the sql query for this?

Id Project_Id Activity_Time Username 1 100 2008-01-01 11:12:13 A 2 100 2008-01-01 00:00:00 B 3 500 2008-02-01 00:00:00 C 4 ...

Grails Hibernate H2 problem

I have an application with two domain classes as follows: DomainA : PK, name DomainB : PK, FK (points to DomainA.PK), name. And when I try to list elements that belongs to DomainA using the ...

Spring configuration for embedded H2 database for tests

What does your Spring configuration for integration tests look like using an embedded h2 datasource and, optionally, JUnit? My first try with a SingleConnectionDataSource basically worked, but failed ...

热门标签