@GeneratedValue(strategy=“IDENTITY") vs. @GeneratedValue(strategy=“SEQUENCE") [英] @GeneratedValue(strategy="IDENTITY") vs. @GeneratedValue(strategy="SEQUENCE")

查看:47
本文介绍了@GeneratedValue(strategy=“IDENTITY") vs. @GeneratedValue(strategy=“SEQUENCE")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是休眠的新手.我不明白以下两个主键生成策略:

I'm new to hibernate. I do not understand the the following two primary key generation strategies:

  1. 身份
  2. 序列

有人可以解释一下这两个是如何工作的以及这两者之间有什么区别吗?

Can someone please explain how these two work and what is the difference between these two?

推荐答案

引用 Java Persistence/Identity and测序:

Identity 排序使用数据库中的特殊 IDENTITY 列,以允许数据库在插入行时自动为对象分配一个 id.许多数据库都支持标识列,例如MySQL、DB2、SQL Server、Sybase 和 Postgres.Oracle 不支持 IDENTITY 列,但可以通过使用序列对象和触发器来模拟它们.

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.数据库引擎会自动为您放置下一个可用值.

In plain English: you mark at most one ID column in your table as IDENTITY. The database engine will put next available value for you automatically.

还有:

Sequence 对象使用特殊数据库对象来生成 ID.序列对象仅在某些数据库中受支持,例如 Oracle、DB2 和 Postgres.通常,SEQUENCE 对象具有名称、INCREMENT 和其他数据库对象设置.每次选择 .NEXTVAL 时,序列都会增加 INCREMENT.

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 are more flexible and slightly more complex. You define an extra object in your database next to tables, triggers, etc. called sequences. Sequences are basically named counter you can use anywhere inside queries.

这篇关于@GeneratedValue(strategy=“IDENTITY") vs. @GeneratedValue(strategy=“SEQUENCE")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆