gpt4 book ai didi

java - @GeneratedValue(strategy ="IDENTITY") 与 @GeneratedValue(strategy ="SEQUENCE")

转载 作者:IT老高 更新时间:2023-10-28 20:57:57 27 4
gpt4 key购买 nike

我是 hibernate 新手。我不明白以下两种主键生成策略:

  1. 身份
  2. 顺序

有人能解释一下这两者是如何工作的吗?这两者有什么区别?

最佳答案

引用 Java Persistence/Identity and Sequencing :

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 <sequence>.NEXTVAL is selected the sequence is incremented by the INCREMENT.

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

关于java - @GeneratedValue(strategy ="IDENTITY") 与 @GeneratedValue(strategy ="SEQUENCE"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8955074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com