有时我发现他们在一起,有时单独……其他时候他们似乎也做同样的事情。
有什么不同?
以下是三个例子。他们的做法有何不同?为什么我不能对所有这些都使用 @GenerateValue?
实施例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 https://en.wikipedia.org/wiki/Object-relational_mapping通常需要生成主键值。
The @GeneratedValue
注释表示列的值,必须用注释@Id
被生成。要素strategy
and generator
注释上描述了如何获取生成的值。
有四个可能的值strategy
上的元素@GeneratedValue
注解:IDENTITY
, AUTO
, TABLE
and SEQUENCE
. See more http://www.objectdb.com/api/java/jpa/GenerationType.
所以要回答Part 2对于你的问题,代码片段表明userId
将通过数据库中的序列获得。
The generator
的元素@GeneratedValue
注释表示主键生成器的名称。在Part1对于你的问题,代码片段表明generator
named increment
将用于获取主键值。increment
然后在下一个注释中定义@GenericGenerator
. @GenericGenerator
是一个 Hibernate 注释,用于表示自定义生成器,它可以是 Hibernate 提供的生成器的类或快捷方式。increment
是 Hibernate 生成器的快捷方式:
生成唯一的 long、short 或 int 类型的标识符
当没有其他进程向同一个表中插入数据时。不要
在集群中使用。
In the 第三方对于你的问题,代码使用hilo
休眠生成器:
使用 hi/lo 算法有效地生成类型标识符
long、short 或 int,给定表和列(默认情况下
hibernate_unique_key 和 next_hi 分别)作为 hi 的来源
价值观。 hi/lo 算法生成唯一的标识符
对于特定的数据库。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)