带有 H2 和 data.sql 的 Spring Boot Data JPA - 找不到表 [英] Spring Boot Data JPA with H2 and data.sql - Table not Found
问题描述
我有一个 Spring Boot 2.5.0 项目.我正在将 spring-data-jap 与 h2 内存数据库一起使用.我想在启动时用 data.sql 文件填充数据,但我得到一个表未找到异常.如果我删除 data.sql 文件,我可以看到我的实体的表会自动创建.但是,如果我包含 data.sql 文件,则会收到错误消息,指出该表不存在.也许是我的 sql 语法错误,我错误地配置了 h2 数据库?
applicaltion.yml
弹簧:数据源:网址:jdbc:h2:mem:test驱动程序类名称:org.h2.Driver用户名:sa密码:sajpa:数据库平台:org.hibernate.dialect.H2Dialect调试:真
data.sql
INSERT INTO BUSINESS_SUMMARY VALUES (1, ALM470", B48", 3);
BusinessSummary.java 实体
@NoArgsConstructor(access = AccessLevel.PROTECTED)@Getter@实体公共类业务摘要{@Id私人长ID;私人字符串businessId;私人字符串businessDomainId;私人整数城市代码;}
BusinessSummaryRepository.java
@Repository公共接口 BusinessSummaryRepository 扩展 JpaRepository<BusinessSummary, Long>{}
例外:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "BUSINESS_SUMMARY";没有找到;SQL语句:INSERT INTO BUSINESS_SUMMARY VALUES(1, ALM470", B48", 3) [42102-200]
spring.jpa.defer-datasource-initialization=true
<块引用>
默认情况下,data.sql 脚本现在在 Hibernate 之前运行初始化.这与基于脚本的基本行为保持一致与 Flyway 和 Liquibase 的初始化.
<块引用>
如果你想使用data.sql 填充由 Hibernate 创建的模式,设置spring.jpa.defer-datasource-initialization 为 true.搅拌时不推荐使用数据库初始化技术,这会还允许您使用 schema.sql 脚本来构建在通过 data.sql 填充之前由 Hibernate 创建的架构.
您必须将 spring.jpa.defer-datasource-initialization 转换为 yml.
I have a Spring Boot 2.5.0 project. I'm using spring-data-jap 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]
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.
这篇关于带有 H2 和 data.sql 的 Spring Boot Data JPA - 找不到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!