适用于postgres serial / bigserial列的Hibernate ID生成器? [英] Proper Hibernate id generator for postgres serial/bigserial column?

查看:127
本文介绍了适用于postgres serial / bigserial列的Hibernate ID生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PostgreSQL表的id为 bigserial ,这意味着它们是在插入行时生成的(因此,id列的值不会在 INSERT 语句)。我无法为我的XML映射文件中的< generator class =...> 属性找到适当的值。

My PostgreSQL tables have id's of type bigserial, meaning they are generated at the time rows are inserted (and thus, the id column's value is not supplied in the INSERT statement). I'm having difficulty finding the proper value for the <generator class="..."> attribute in my XML mapping file.

下面的代码是我发现的最接近Postgres的,但它仍然执行 SELECT nextval(...)插入之前的序列(并在插入中明确包含id字段的值)。我只希望Hibernate根本不包含id字段值,这样Postgres就可以完成自己生成值的工作。

The code below is the closest I've found that seems to be the closest for Postgres, but it's still performing a SELECT nextval(...) on the sequence before inserting (and explicitly including the id field's value on the insert). I just want Hibernate to not include the id field value at all, allowing Postgres to do its job of generating the value itself.

    <id name="id" column="id" type="java.lang.Long">
        <generator class="sequence">
            <param name="sequence">my_sequence_name</param>
        </generator>
    </id>


推荐答案

这是无证的,但实际上可以使用<$当PK的类型为 SERIAL BIGSERIAL 时,使用PostgreSQL创建c $ c> identity :

This is undocumented but you can actually use an identity generator with PostgreSQL when the PK is of type SERIAL or BIGSERIAL:

<id name="id" column="user_id" type="java.lang.Long">
     <generator class="identity"/>
</id>

请参阅 HB-875 and HHH-1675 作为背景。

这篇关于适用于postgres serial / bigserial列的Hibernate ID生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆