【发布时间】:2015-10-01 09:08:16
【问题描述】:
我尝试使用 Bypass GeneratedValue in Hibernate (merge data not in db?) 中的自定义 id-generator,它在使用 Postgres DB 时运行良好。我的代码等于示例中的代码。 但是在使用 H2 内存数据库运行测试时,我遇到了问题,该 id 不是自动生成的。
没有自定义生成器
@Column(name = "id", nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
生成的创建表脚本
create table db.schema.entity (id bigserial not null...
带有自定义生成器
@Column(name = "id", nullable = false)
@Id
@GeneratedValue(generator = "idGenerator", strategy = GenerationType.IDENTITY)
@GenericGenerator(name="idGenerator", strategy = "...UseIdOrGenerate")
private Long id;
生成的创建表脚本
create table db.schema.entity (id int8 not null...
因此测试不起作用。
【问题讨论】:
-
你的测试怎么样?
-
@LeonidGlanz 简单 junit 测试:创建并持久化实体,然后检查该实体是否存在于数据库中。