关于java:@GeneratedValue(strategy =” IDENTITY”)与@GeneratedValue(strategy =” SEQUENCE”)

@GeneratedValue(strategy=“IDENTITY”) vs. @GeneratedValue(strategy=“SEQUENCE”)

我是新来的Hibernate者。我不了解以下两种主要的密钥生成策略:

  • 身份
  • 顺序
  • 请问有人可以解释这两个是如何工作的,两者之间有什么区别?


    引用Java持久性/标识和顺序:

    Identity sequencing uses special IDENTITY columns in the database to allow the database to automatically assign an id to the object when its row is inserted. Identity columns are supported in many databases, such as MySQL, DB2, SQL Server, Sybase and Postgres. Oracle does not support IDENTITY columns but they can be simulated through using sequence objects and triggers.

    用简单的英语:您将表中最多一个ID列标记为IDENTITY。数据库引擎将自动为您提供下一个可用值。

    并且:

    Sequence objects use special database objects to generate ids. Sequence objects are only supported in some databases, such as Oracle, DB2, and Postgres. Usually, a SEQUENCE object has a name, an INCREMENT, and other database object settings. Each time the .NEXTVAL is selected the sequence is incremented by the INCREMENT.

    序列更灵活,稍微更复杂。您可以在数据库中的表,触发器等旁边定义一个额外的对象,称为序列。序列基本上被命名为计数器,您可以在查询中的任何地方使用它。