【发布时间】:2020-08-08 18:40:48
【问题描述】:
我在运行 SpringBootTests 时遇到了问题。
测试使用 H2 数据库,因此每次运行时都会重新创建架构。 对于我的一个实体,Hibernate 不会将 auto_increment 添加到 id 列。
我找不到失败的实体 (Payment) 和其他正常工作的实体 (例如 Invoice) 之间的任何相关差异。他们在id字段上都有如下注解:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
我设置了spring.jpa.show-sql=true,这就是我看到的:
-
Payment实体显示create table payment (id bigint not null, -
Invoice实体显示create table invoice (id bigint not null auto_increment,
其实我尝试将Payment类复制到Payment2,Payment2没有问题:
-
Payment实体显示create table payment (id bigint not null, -
Payment2实体显示create table invoice (id bigint not null auto_increment,
鉴于此,我相信我应该在Payment 类中寻找一个不是 的覆盖配置,但它会以某种方式从该类中删除auto_increment。
有人知道是什么原因造成的吗?
这些是所涉及的库的版本:
- Spring Boot 2.1.8 版
- 休眠 5.3.11
- H2 数据库 1.4.200
【问题讨论】:
-
你试过了吗(策略= GenerationType.AUTO)
-
检查数据库中的 id 列。是自增吗?
-
@tashi AUTO 在运行 MySQL 时会中断(H2 仅用于测试)。此外,每个可能相关的答案都说要从 AUTO 转向 IDENTITIY。
-
@GolamMazidsajib 正如我在问题中所说:“测试使用的是 H2 数据库,因此每次运行时都会重新创建模式。”没有与我的代码匹配的列的数据库。
-
@tashi 并不是要忽略您的评论。我试着改变它只是为了看看它是否会有所作为。它没; AUTO vs IDENTITY 似乎对此没有影响。
标签: spring hibernate spring-boot h2 spring-boot-test