简体   繁体   English

带有 SequenceGenerator 和 GeneratedValue 的 JPA

[英]JPA with SequenceGenerator and GeneratedValue

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 序列用于生成 PK.Am 使用 Oracle DB

@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.现在我对此的理解是:DB sequencer 返回的sequence id 用作rec_id。 IS this correct?这是正确的吗?

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). @SequenceGenerator 注解用于定义序列并接受名称、初始值(默认为 1)和分配大小(默认为 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: 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 (ie before commit).与 AUTO 和 IDENTITY 不同,一旦新的实体对象被持久化(即在提交之前),SEQUENCE 策略就会自动生成一个值。 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.为了尽量减少到数据库服务器的往返次数,ID 是按组分配的。 The number of IDs in each allocation is specified by the allocationSize attribute.每个分配中的 ID 数量由 allocationSize 属性指定。 It is possible that some of the IDs in a given allocation will not be used.给定分配中的某些 ID 可能不会被使用。 Therefore, this strategy does not guarantee there will be no gaps in sequence values.因此,此策略并不能保证序列值中没有间隙。

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。我希望这是有道理的。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM