@GeneratedValue和@GenericGenerator之间的区别 [英] Difference between @GeneratedValue and @GenericGenerator
问题描述
有什么区别?
这里有三个例子。他们做什么不同?为什么我不能只用@GeneratedValue来处理它们?
示例1
@Id
@GeneratedValue(generator =increment)
@GenericGenerator(name =increment,strategy =increment)
Long id; b
$ b 例子2 code> @Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private int userId; b
$ b
示例3 code> @ElementCollection
@JoinTable(name =Address,
joinColumns = @ JoinColumn(name =user_id)
)
@GenericGenerator(name = hilo-gen,strategy =hilo)
@CollectionId(columns = @Column(name =Address_id),generator =hilo-gen,type = @Type(type =long))
Collection< Addr> listOfAddresses = new ArrayList< Addr>();
当使用 ORM 通常需要生成主键值。
@GeneratedValue
注释表示必须为 @Id
注释的列的值被生成。注释中的元素 strategy
和 generator
描述如何获得生成的值。
@GeneratedValue
注释中的策略
元素有四个可能的值: IDENTITY
, AUTO
, TABLE
和 SEQUENCE
。 查看更多。
因此,要回答您的问题的第2部分,代码片段指示 userId
的值将通过数据库中的序列获得。
@GeneratedValue
注释的生成器
元素表示主键生成器的名称。在你的问题的 Part1 中,代码片断表明名为 increment
的生成器
将会是用于获取主键值。然后在下一个注释 @GenericGenerator
中定义 increment
。 @GenericGenerator
是一个用于表示自定义生成器的hibernate注释,它可以是由Hibernate提供的生成器的类或快捷方式。 increment
是Hibernate生成器的一个快捷方式:
生成long类型的标识符,当没有其他进程将数据插入到同一个表中时,short或int是唯一的
。不要
在群集中使用。
在您的问题的第三方中,代码使用 hilo
Hibernate生成器:
lockquote>
使用hi / lo算法高效地生成类型为
long,short或int的标识符,给定一个表和列(分别默认为
hibernate_unique_key和next_hi)作为hi
值的源。 hi / lo算法生成的标识符对于特定数据库来说只是
唯一的标识符。
Sometimes I find them together, sometimes alone... other times they seem to do the same.
What's the difference?
Here are three examples. What do they do of different? Why can't I use just @GeneratedValue for all of them?
Example 1
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
Long id;
Example 2
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
private int userId;
Example 3
@ElementCollection
@JoinTable(name="Address",
joinColumns=@JoinColumn(name="user_id")
)
@GenericGenerator(name="hilo-gen", strategy="hilo")
@CollectionId(columns = @Column(name="Address_id"), generator = "hilo-gen", type = @Type(type="long"))
Collection<Addr> listOfAddresses = new ArrayList<Addr>();
When using an ORM it is often necessary to generate a primary key value.
The @GeneratedValue
annotation denotes that a value for a column, which must be annotated with @Id
is generated. The elements strategy
and generator
on the annotation describe how the generated value is obtained.
There are four possible values for the strategy
element on the @GeneratedValue
annotation: IDENTITY
, AUTO
, TABLE
and SEQUENCE
. See more.
So to answer Part 2 of your question, the code snippet is indicating that the value of userId
will be obtained through a sequence in the database.
The generator
element of the @GeneratedValue
annotation denotes the name of the primary key generator. In Part1 of your question, the code snippet indicates that a generator
named increment
will be used to obtain the primary key value. increment
is then defined in the next annotation @GenericGenerator
. @GenericGenerator
is a hibernate annotation used to denote a custom generator, which can be a class or shortcut to a generator supplied by Hibernate. increment
is a shortcut to a Hibernate generator that:
generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
In the Third Part of your question, the code uses a hilo
Hibernate generator that:
uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database.
这篇关于@GeneratedValue和@GenericGenerator之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!