【问题标题】:H2 in-memory database and custom @GenericGenerator strategyH2 内存数据库和自定义 @GenericGenerator 策略
【发布时间】: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 测试:创建并持久化实体,然后检查该实体是否存在于数据库中。

标签: java hibernate jpa h2


【解决方案1】:

通过改变@Column解决

@Column(name = "id", nullable = false, columnDefinition = "bigserial")

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 2018-08-05
    相关资源
    最近更新 更多