带有GenerationType.IDENTITY的JPA和PostgreSQL [英] JPA and PostgreSQL with GenerationType.IDENTITY

查看:115
本文介绍了带有GenerationType.IDENTITY的JPA和PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Postgres和GenerationType.Identity vs Sequence的问题

在这个例子中...

<$>

<$>
@SequenceGenerator(name =mytable_id_seq,
sequenceName =mytable_id_seq,
allocationSize = 1)
@GeneratedValue( strategy = GenerationType.SEQUENCE,
generator =mytable_id_seq)

据我所知,我是指定Postgres序列以通过注释来使用。然而,我有一个用'serial'类型定义的id列,我已经读过,我可以简单地使用GenerationType.IDENTITY它会自动生成一个数据库序列,并使用它来自动增加。



如果是这种情况,我认为使用SEQUENCE注释是没有好处的,除非你是使用一个整数为一个ID或有一些具体的理由使用您创建的另一个序列。 IDENTITY的代码非常少,可能会使它跨数据库移植。



是否有某些我错过的东西?



感谢您的反馈。 SERIAL 类型的列,只需使用以下内容注释您的 id 字段就足够了:

  @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

这是告诉Hibernate数据库将关注 id 列的生成。数据库如何实现自动生成是特定于供应商的,并且可以被认为是对Hibernate透明的。 Hibernate只需要知道插入行后,该行就会有一个 id 值,它可以以某种方式检索。



如果使用 GenerationType.SEQUENCE ,您告诉Hibernate数据库不会自动填充 id 柱。相反,Hibernate有责任从指定的序列中获取下一个序列值,并在插入行时使用该值作为 id 值。因此,Hibernate正在生成并插入 id



对于Postgres,恰好定义了一个 SERIAL 列通过创建一个序列并将其用作默认列值来实现。但它是填充 id 字段的数据库,因此使用 GenerationType.IDENTITY 告诉Hibernate数据库正在处理id 。

这些引用可能有助于:

http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#标识符生成器



https://www.postgresql.org/docs/8.1/static/datatype.html#DATATYPE-SERIAL


I have a question about Postgres and GenerationType.Identity vs Sequence

In this example...

@Id
@SequenceGenerator(name="mytable_id_seq",
                   sequenceName="mytable_id_seq",
                   allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
                generator="mytable_id_seq")

I understand that I am specifying a Postgres sequence to use via annotations.

However, I have an id column defined with the 'serial' type, I have read that I can simply use GenerationType.IDENTITY and it will automatically generate a db sequence and use it to auto increment.

If that's the case, I don't see an advantage to using the SEQUENCE annotations unless you are using an integer for an id or have some specific reason to use another sequence you have created. IDENTITY is alot less code and potentially makes it portable across databases.

Is there something I'm missing?

Thanks in advance for the feedback.

解决方案

If you have a column of type SERIAL, it will be sufficient to annotate your id field with:

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)

This is telling Hibernate that the database will be looking after the generation of the id column. How the database implements the auto-generation is vendor specific and can be considered "transparent" to Hibernate. Hibernate just needs to know that after the row is inserted, there will be an id value for that row that it can retrieve somehow.

If using GenerationType.SEQUENCE, you are telling Hibernate that the database is not automatically populating the id column. Instead, it is Hibernate's responsibility to get the next sequence value from the specified sequence and use that as the id value when inserting the row. So Hibernate is generating and inserting the id.

In the case of Postgres, it happens that defining a SERIAL column is implemented by creating a sequence and using it as a default column value. But it is the database that is populating the id field so using GenerationType.IDENTITY tells Hibernate that the database is handling id generation.

These references may help:

http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators

https://www.postgresql.org/docs/8.1/static/datatype.html#DATATYPE-SERIAL

这篇关于带有GenerationType.IDENTITY的JPA和PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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