看如下代码:
Figure 1. Sequencing the identifier of an Entity
为一个Entity定义自增ID字段的大概步骤分为两步:
1、定义Sequence自增序列;
2、将定义好的Sequence序列设置需要自增的字段。
使用注释@SequenceGenerator定义Sequence
上图中标记(1)处定义了JPA中此Sequence的name;标记(2)处定义了数据库中此Sequence的name;标记(3)处定义了此Sequence的起始值;标记(4)处定义了增量即Sequence的增长步长。
使用定义好的Sequence
上面我们已经定义好了Sequence,现在就可以使用这个Sequence来定义我们的自增字段。标记(5)处告诉JPA我们将使用上一步定义好的Sequence by referring to its name defined above at the mark(1). The attribute at mark(6) tells JPA that the generator we're gonna use is a SEQUENCE.