【发布时间】:2021-08-17 01:07:55
【问题描述】:
我有一个 Spring Boot 2.5.0 项目。我将 spring-data-jap 与 h2 内存数据库一起使用。我想在启动时使用 data.sql 文件填充数据,但我得到了一个找不到表的异常。如果我删除 data.sql 文件,我可以看到我的实体的表确实是自动创建的。但是,如果我包含 data.sql 文件,则会收到错误消息,指出该表不存在。也许是我的 sql 语法错误,我错误配置了 h2 数据库?
应用程序.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 实体
@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> {
}
例外:
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-boot spring-data-jpa h2