Java - JPA - 生成器 - @SequenceGenerator [英] Java - JPA - Generators - @SequenceGenerator

查看:140
本文介绍了Java - JPA - 生成器 - @SequenceGenerator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习JPA并且在 @SequenceGenerator 注释中感到困惑。

I am learning JPA and have confusion in the @SequenceGenerator annotation.

根据我的理解,它会自动为实体的数字标识字段/属性赋值。

To my understanding, it automatically assigns a value to numeric identity fields/properties of an entity.

Q1。此序列生成器是否利用数据库增加的数值生成功能或自己生成数字?

Q1. Does this sequence generator make use of the database's increasing numeric value generating capability or generates the number on it's own?

Q2。如果JPA使用数据库自​​动增量功能,那么它是否可以使用数据存储有自动增量功能吗?

Q2. If JPA uses database auto increment feature, then will it work with datastores that don't have auto increment feature?

Q3。如果JPA自己生成数值,那么JPA实现如何知道要生成哪个值下一个?是否先咨询数据库以查看最后存储的值以生成值(最后+1)?

Q3. If JPA generate numeric value on his own, then how does the JPA implementation know which value to generate next? Does it consult with the database first to see what value was stored last in order to generate the value (last + 1)?



Q4。请详细说明 sequenceName allocationSize 的属性@SequenceGenerator 注释。


Q4. Please also shed some light on sequenceName and allocationSize properties of @SequenceGenerator annotation.

推荐答案

sequenceName 是DB中序列的名称。这是指定DB中已存在的序列的方法。如果你走这条路线,你必须指定 allocationSize ,它必须与DB序列用作自动增量的值相同。

sequenceName is the name of the sequence in the DB. This is how you specify a sequence that already exists in the DB. If you go this route, you have to specify the allocationSize which needs to be the same value that the DB sequence uses as its "auto increment".

用法:

@GeneratedValue(generator="my_seq")
@SequenceGenerator(name="my_seq",sequenceName="MY_SEQ", allocationSize=1)

如果需要,可以让它为您创建序列。但要做到这一点,您必须使用SchemaGeneration来创建它。为此,请使用:

If you want, you can let it create a sequence for you. But to do this, you must use SchemaGeneration to have it created. To do this, use:

@GeneratedValue(strategy=GenerationType.SEQUENCE)

此外,您可以使用自动生成,它将使用表格生成ID。使用此功能时,您还必须在某些时候使用SchemaGeneration,因此可以创建生成器表。为此,请使用:

Also, you can use the auto-generation, which will use a table to generate the IDs. You must also use SchemaGeneration at some point when using this feature, so the generator table can be created. To do this, use:

@GeneratedValue(strategy=GenerationType.AUTO)

这篇关于Java - JPA - 生成器 - @SequenceGenerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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