postgresql postgres 串行/大串行列的正确 Hibernate id 生成器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3433724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:14:42  来源:igfitidea点击:

Proper Hibernate id generator for postgres serial/bigserial column?

javahibernatepostgresqlmappinghibernate-mapping

提问by Matt Huggins

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 INSERTstatement). I'm having difficulty finding the proper value for the <generator class="...">attribute in my XML mapping file.

我的 PostgreSQL 表的 id 类型为bigserial,这意味着它们是在插入行时生成的(因此,INSERT语句中未提供 id 列的值)。我很难<generator class="...">在我的 XML 映射文件中为属性找到正确的值。

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.

下面的代码是我发现的最接近的代码,它似乎与 Postgres 最接近,但它仍然SELECT nextval(...)在插入之前对序列执行 a (并在插入中明确包含 id 字段的值)。我只希望 Hibernate 根本不包含 id 字段值,从而允许 Postgres 自行生成值。

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

回答by Pascal Thivent

This is undocumented but you can actually use an identitygenerator with PostgreSQL when the PK is of type SERIALor BIGSERIAL:

这是未记录的,但是identity当 PK 类型为SERIALor时,您实际上可以将生成器与 PostgreSQL 一起使用BIGSERIAL

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

See HB-875and HHH-1675for background on this.

有关这方面的背景信息,请参阅HB-875HHH-1675

回答by R?zvan Flavius Panda

From what i read:

从我读到的:

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

should work faster than:

应该比以下工作更快:

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

The sequence generator falls in the Non-insert POID generatorsdescribed like this:

序列生成器属于如下描述的非插入 POID 生成器

Non-insert POID generators are the best option for new applications. These generators allow NHibernate to assign an identity to a persistent object without writing the object's data to thedatabase, allowing NHibernate to delay writing until the business transaction is complete, reducing round trips to the database.

非插入式 POID 发生器是新应用的最佳选择。这些生成器允许 NHibernate 为持久对象分配一个身份,而无需将对象的数据写入数据库,从而允许 NHibernate 延迟写入直到业务事务完成,从而减少到数据库的往返行程。

While the identity generator is Post-insert POID generatorsgroup:

而身份生成器是Post-insert POID 生成器组:

Post-insert POID generators require data to be persisted to the database for an ID to be generated. This alters the behavior of NHibernate in very subtle ways and disables some performance features. As such, use of these POID generators is strongly discouraged!They should only be used with existing databases where other applications rely on this behavior.

插入后 POID 生成器需要将数据持久化到数据库以生成 ID。这以非常微妙的方式改变了 NHibernate 的行为并禁用了一些性能特性。因此,强烈建议不要使用这些 POID 生成器!它们只应与其他应用程序依赖此行为的现有数据库一起使用。

Quotes were taken from NHibernate 3.0 Cookbook.

引自 NHibernate 3.0 Cookbook。

回答by YoK

Tried following and it worked:

尝试了以下方法,它奏效了:

<id name="id" column="id" type="long" unsaved-value="null" >
        <generator class="sequence">
            <param name="sequence">my_sequence_name</param>
        </generator>
</id>