@GeneratedValue 和 @GenericGenerator 的区别 [英] Difference between @GeneratedValue and @GenericGenerator

查看:66
本文介绍了@GeneratedValue 和 @GenericGenerator 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我发现他们在一起,有时单独……有时他们似乎也做同样的事情.

Sometimes I find them together, sometimes alone... other times they seem to do the same.

有什么区别?

这里有三个例子.他们有什么不同?为什么我不能对所有这些只使用 @GeneratedValue?

Here are three examples. What do they do of different? Why can't I use just @GeneratedValue for all of them?

示例 1

@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment") 
Long id;

示例 2

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
private int userId;

示例 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>();

推荐答案

使用 ORM 时 通常需要生成主键值.

When using an ORM it is often necessary to generate a primary key value.

@GeneratedValue 注释表示为列生成了一个值,该值必须用 @Id 进行注释.注解上的元素 strategygenerator 描述了如何获得生成的值.

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.

@GeneratedValue 注释上的 strategy 元素有四个可能的值:IDENTITYAUTOTABLESEQUENCE.查看更多.

There are four possible values for the strategy element on the @GeneratedValue annotation: IDENTITY, AUTO, TABLE and SEQUENCE. See more.

因此,为了回答您问题的第 2 部分,代码片段表明 userId 的值将通过数据库中的序列获得.

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.

@GeneratedValue 注释的 generator 元素表示主键生成器的名称.在您的问题的 Part1 中,代码片段表明将使用名为 incrementgenerator 来获取主键值.increment 然后在下一个注解@GenericGenerator 中定义.@GenericGenerator 是一个hibernate注解,用于表示自定义生成器,可以是Hibernate提供的类或者生成器的快捷方式.increment 是 Hibernate 生成器的快捷方式:

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:

生成仅唯一的 long、short 或 int 类型的标识符当没有其他进程将数据插入同一个表时.不要在集群中使用.

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.

在您问题的第三部分中,代码使用了一个 hilo Hibernate 生成器:

In the Third Part of your question, the code uses a hilo Hibernate generator that:

使用 hi/lo 算法有效地生成类型标识符long、short 或 int,给定一个表和列(默认情况下hibernate_unique_key 和 next_hi 分别)作为 hi 的来源值.hi/lo 算法生成唯一的标识符用于特定数据库.

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屋!

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