首页 > 解决方案 > Hibernate 在尝试使用生成的 UUID 持久化 DTO 时不断询问 hibernate_sequence

问题描述

我正在使用 dropwizard-hibernate 和 postgres(休眠版本 5.3.7)

对于我的 DTO,我有一个包含 ID 字段的基本 DTO(所有 DTOS 都扩展了此类)

在数据库模式中,ID 看起来像这样

id uuid default gen_random_uuid() not null

我对 ID 的配置是这样的:

    @Id 
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid2")
    private UUID id;

从理论上讲,这应该可以,但是每次我尝试坚持一个实体时,我都会收到一个错误消息

ERROR: relation \"hibernate_sequence\" does not exist\

我已经尝试了一切,但没有任何效果..我只尝试了@Id和@GeneratedValue(根据最新的休眠文档,对于UUID配置应该足够了)和许多其他注释组合但每次我尝试持久化实体我得到了缺少序列的问题。

我知道我可以“修复它”只需在数据库中添加 hibernate_sequence 表,但我根本不需要它。

标签: postgresqlhibernatejpauuiddropwizard

解决方案


我已经使用了它,它按预期工作:

@Column(name = "uid")
@Generated(GenerationTime.ALWAYS)
@Type(type = "pg-uuid")
private UUID uid;

我意识到这@Generated是一个遗留注释,但它似乎有效。


推荐阅读