JPA SequenceGenerator和GeneratedValue:名称/生成器属性仅每个类唯一? [英] JPA SequenceGenerator and GeneratedValue: name / generator property unique only per class?

查看:439
本文介绍了JPA SequenceGenerator和GeneratedValue:名称/生成器属性仅每个类唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将JPA + OpenJPA与PostgreSQL一起用作后端RDBMS.我的表的主键通常由SERIAL/BIGSERIAL列组成.因此PostgreSQL自动为新条目(strategy=GenerationType.IDENTITY)生成ID.

I use JPA + OpenJPA with PostgreSQL as backend RDBMS. The primary keys of my tables usually consist of a SERIAL / BIGSERIAL column. So PostgreSQL automatically generates IDs for new entries (strategy=GenerationType.IDENTITY).

ID属性的注释如下:

The annotations of ID property look like this:

@Id
@SequenceGenerator(name="myseq",sequenceName="foobartable_fooid_seq")
@GeneratedValue(generator="myseq",strategy=GenerationType.IDENTITY)

我的问题是:我是否可以在仅修改sequenceName值的情况下将此注释块复制并粘贴到多个实体? sequenceName在表之间是不同的.但是,是否可以将所有实体的SequenceGenerator命名为myseq或类似的名称?还是必须为每个实体的SequenceGenerator指定唯一的生成器名称?这样,每个SequenceGenerator名称在持久性单元中必须是唯一的吗?

My question is: Can I copy and paste this annotations block to several entities, while only modifying the sequenceName value? The sequenceName is different from table to table. But may the SequenceGenerator of all Entities be named myseq or something like that? Or does one have to specify a unique generator name for the SequenceGenerator of each Entity? So that each SequenceGenerator name must be unique in the persistence unit?

使用与数据库中的sequenceName相同的值可能是一个好主意吗?这样我写类似

Is it possibly a good idea to use the same value used for the sequenceName in the database? So that I'd write something like

@Id
@SequenceGenerator(name="foobartable_fooid_seq",sequenceName="foobartable_fooid_seq")
@GeneratedValue(generator="foobartable_fooid_seq",strategy=GenerationType.IDENTITY)

关于如何命名SequenceGenerators的任何建议?

Any recommendations on how to name the SequenceGenerators?

在此先感谢您的任何建议!

Many thanks in advance for any suggestions!

您, 斯努布先生

推荐答案

来自 Javadoc 用于SequenceGenerator,重点是我的:

From the Javadoc for SequenceGenerator, emphasis is mine:

定义主键生成器,当 为GeneratedValue注释指定了generator元素.一种 序列生成器可以在实体类上或在 主键字段或属性. 生成器名称的范围是 持久性单元的全局变量(适用于所有生成器类型).

Defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A sequence generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).

因此,您将要为在持久性单元中定义的每个序列使用唯一的名称.如上注释中所述,在切线上,您的GeneratedValue策略应为SEQUENCE.

So you will want to use a unique name for each sequence defined in a persistence unit. On a tangent, your GeneratedValue strategy should be SEQUENCE, as pointed out in a comment above.

这篇关于JPA SequenceGenerator和GeneratedValue:名称/生成器属性仅每个类唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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