升级到Boot2.7之后,使用嵌入式H2数据库的集成测试开始失败。
我在日志中看到了这条警告消息,但不太清楚原因或解决方案:
WARN 8053 ---[ main] o.h.t.s.i.ExceptionHandlerLoggedImpl :GenerationTarget encountered exception accepting command : Error executing DDL "create table user (id bigint generated by default as identity, email varchar(255) not null, name varchar(255), primary key (id))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table user (id bigint generated by default as identity, email varchar(255) not null, name varchar(255), primary key (id))" via JDBC Statement
...
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "create table [*]user (id bigint generated by default as identity, email varchar(255) not null, name varchar(255), primary key (id))"; expected "identifier"; SQL statement:
create table user (id bigint generated by default as identity, email varchar(255) not null, name varchar(255), primary key (id)) [42001-212]
...
我的User
表似乎不是在升级之后创建的,因此我的测试失败了。
发布于 2022-06-08 13:31:36
Boot2.7似乎升级到了它的H2依赖项2.x,这是不向后兼容的,并引入了一些更改:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#h2-21
问题是,User
现在是一个关键字/保留词(在我的例子中,H2“迁移到-v2”指南不是很有用;它提到添加了新的关键字,但它没有提供指向:
https://www.h2database.com/html/advanced.html#keywords
因此,我需要做的是使用“引证姓名”来定义实体的表名(似乎我也可以在表注释中使用backticks来代替转义双引号):
@Table(name="\"user\"")
@Entity
public class User {
...
对于这个表,我还必须对我的data.sql
文件使用双引号:
INSERT INTO "user"(id, email, name) VALUES(1, 'test@user.com', 'Test User');
注意:迁移指南还提到了使用命令作为解决方法的可能性,但也不鼓励它。
https://stackoverflow.com/questions/72546596
复制相似问题