简体   繁体   English

Oracle:在@GeneratedValue 注释的情况下如何生成主键id

[英]Oracle : How primary key id is generated in case of @GeneratedValue annotation

So I have a repository class in which I can see the primary key field has a annotation所以我有一个存储库类,我可以在其中看到主键字段有一个注释

@Id
@GeneratedValue

So, what I know that when the strategy is not explicitly defined the default strategy set is auto.所以,我知道当策略没有明确定义时,默认策略集是自动的。 And in the case of auto strategy the undermine database decides the strategy from: Sequence, Table, Identity.在自动策略的情况下,破坏数据库从以下方面决定策略:序列、表、身份。

So, my database is Oracle, in case of Oracle, sequence is the most preferred strategy.所以,我的数据库是 Oracle,在 Oracle 的情况下,序列是最优选的策略。

Correct me if I am wrong.如果我错了,请纠正我。

My question is how I can know which strategy is used by my table, is it sequence and if sequence, is it table specific or universal.我的问题是我如何知道我的表使用了哪种策略,它是序列,如果是序列,它是特定于表还是通用的。

And is this sequence ensuring that the new id is greater than max id.这个序列是否确保新 id 大于 max id。

I am quite new in this field, and not able to find any useful resource for understanding this.我在这个领域很新,无法找到任何有用的资源来理解这一点。

Thanks for your help in advance.提前感谢您的帮助。 :) :)

I'm quite sure it is table specific sequences if you configured ddl auto.如果您配置了 ddl auto,我很确定它是特定于表的序列。 Search for JPA in Google to find a bunch of articles about the subject like :在 Google 中搜索 JPA 以找到一堆关于该主题的文章,例如:

https://www.objectdb.com/java/jpa/entity/generated https://www.objectdb.com/java/jpa/entity/generated

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
// Define a sequence - might also be in another class:
@SequenceGenerator(name="seq", initialValue=1, allocationSize=100)
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.因此,此策略并不能保证序列值中没有间隙。

The Table Strategy表策略

The TABLE strategy is very similar to the SEQUENCE strategy: TABLE 策略与 SEQUENCE 策略非常相似:

@Entity
@TableGenerator(name="tab", initialValue=0, allocationSize=50)
public class EntityWithTableId {
    @GeneratedValue(strategy=GenerationType.TABLE, generator="tab")
    @Id long id;
}

ORM-based JPA providers (such as Hibernate, TopLink, EclipseLink, OpenJPA, JPOX, etc.) simulate a sequence using a table to support this strategy.基于 ORM 的 JPA 提供程序(例如 Hibernate、TopLink、EclipseLink、OpenJPA、JPOX 等)使用表模拟序列以支持此策略。 ObjectDB does not have tables, so the TABLE and SEQUENCE strategies are almost identical. ObjectDB 没有表,因此 TABLE 和 SEQUENCE 策略几乎相同。

A tiny difference is related to the initial value attribute.一个微小的差异与初始值属性有关。 Whereas the SEQUENCE strategy maintains the next sequence number to be used the TABLE strategy maintains the last value that was used. SEQUENCE 策略维护要使用的下一个序列号,而 TABLE 策略维护最后使用的值。 The implication for the initialValue attribute is that if you want sequence numbers to start with 1 in the TABLE strategy initialValue=0 has to be specified in the @SequenceGenerator annotation. initialValue 属性的含义是,如果您希望序列号在 TABLE 策略中以 1 开头,则必须在 @SequenceGenerator 注释中指定 initialValue=0。

or : https://vladmihalcea.com/why-you-should-never-use-the-table-identifier-generator-with-jpa-and-hibernate/或: https : //vladmihalcea.com/why-you-should-never-use-the-table-identifier-generator-with-jpa-and-hibernate/

Even this article that explains that sequence strategy have far best performances with benchmarked code snippets : https://vladmihalcea.com/why-you-should-never-use-the-table-identifier-generator-with-jpa-and-hibernate/甚至这篇解释序列策略的文章在基准代码片段中也有最好的表现: https : //vladmihalcea.com/why-you-should-never-use-the-table-identifier-generator-with-jpa-and-hibernate /

If your stack is spring-data-jpa they have a very good documentation on pivotal.如果您的堆栈是 spring-data-jpa,他们有关于关键的非常好的文档。 https://spring.io/projects/spring-data-jpa https://spring.io/projects/spring-data-jpa

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

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