Java 带有 SequenceGenerator 和 GeneratedValue 的 JPA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20490389/
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
JPA with SequenceGenerator and GeneratedValue
提问by user93796
I have a code which uses JPA annotations to generate DB primary key.A DB sequence is used to generate the PK.Am using Oracle DB
我有一个代码,它使用 JPA 注释生成 DB 主键。DB 序列用于生成使用 Oracle DB 的 PK.Am
@Id
@Column(name = "rec_id", scale = 0)
@GeneratedValue(generator = "RecIdSequence", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "RecIdSequence", sequenceName = "P_REC_ID_SEQUENCE")
public Long getRecordId() {
return outboundPackageRecordId;
}
Now my understanding of this is: sequence id returned by DB sequencer is used as rec_id. IS this correct?
现在我对此的理解是:DB sequencer 返回的sequence id 用作rec_id。这样对吗?
DOC says:
DOC 说:
The Sequence Strategy The sequence strategy consists of two parts - defining a named sequence and using the named sequence in one or more fields in one or more classes. The @SequenceGenerator annotation is used to define a sequence and accepts a name, an initial value (the default is 1) and an allocation size (the default is 50). A sequence is global to the application and can be used by one or more fields in one or more classes. The SEQUENCE strategy is used in the @GeneratedValue annotation to attach the given field to the previously defined named sequence:
序列策略 序列策略由两部分组成 - 定义命名序列和在一个或多个类的一个或多个字段中使用命名序列。@SequenceGenerator 注解用于定义序列并接受名称、初始值(默认为 1)和分配大小(默认为 50)。序列对于应用程序是全局的,并且可以被一个或多个类中的一个或多个字段使用。SEQUENCE 策略用于 @GeneratedValue 注释中,以将给定字段附加到先前定义的命名序列:
@Entity
@SequenceGenerator(name="seq", initialValue=1, allocationSize=100) // Define a sequence - might also be in another class:
public class EntityWithSequenceId {
// Use the sequence that is defined above:
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
@Id long id;
}
Unlike AUTO and IDENTITY, the SEQUENCE strategy generates an automatic value as soon as a new entity object is persisted (i.e. before commit). This may be useful when the primary key value is needed earlier. To minimize round trips to the database server, IDs are allocated in groups. The number of IDs in each allocation is specified by the allocationSize attribute. It is possible that some of the IDs in a given allocation will not be used. Therefore, this strategy does not guarantee there will be no gaps in sequence values.
与 AUTO 和 IDENTITY 不同,一旦新的实体对象被持久化(即在提交之前),SEQUENCE 策略就会自动生成一个值。当更早需要主键值时,这可能很有用。为了尽量减少到数据库服务器的往返次数,ID 是按组分配的。每个分配中的 ID 数量由 allocationSize 属性指定。给定分配中的某些 ID 可能不会被使用。因此,此策略并不能保证序列值中没有间隙。
回答by Makky
This will use the next value from your sequence P_REC_ID_SEQUENCE
.
这将使用序列中的下一个值P_REC_ID_SEQUENCE
。
This also depends upon what database you're using. You can define Sequences in Postgres
but not MySQL
.
这也取决于您使用的数据库。您可以在 中定义序列,Postgres
但不能在 中定义MySQL
。
If you are using MYSQL
then you can use Auto-Incremement
but you will not need to define the sequence .
如果您正在使用,MYSQL
那么您可以使用Auto-Incremement
但您不需要定义序列。
If you're using ORACLE
database you define sequence as
如果您使用ORACLE
数据库,则将序列定义为
CREATE SEQUENCE emp_sequence
INCREMENT BY 1
START WITH 1
Now, if the current sequence value is 100 then the next one will be 101. I hope it makes sense.
现在,如果当前序列值是 100,那么下一个将是 101。我希望这是有道理的。
回答by SreeNath
Use generation type as AUTO instead SEQUENCE on primary key column.
在主键列上使用生成类型作为 AUTO 而不是 SEQUENCE。
@GeneratedValue(strategy = GenerationType.AUTO, generator = "generator")
@SequenceGenerator(name="generator", sequenceName="DB_SEQ_NAME")