一、SpringDataJpa标准用法
spring data jpa 的标准主键生成策略主要有四种,分别是:
public enum GenerationType {
TABLE, //使用一个额外的数据库表来保存主键
SEQUENCE,//使用序列的方式,且其底层数据库要支持序列,一般有postgres、Oracle等
IDENTITY,//主键由数据库生成,一般为自增型主键,支持的有MySql和Sql Server
AUTO//由程序来决定主键规则
}
主键的生成规则主要由注解 @GeneratedValue 来说明,其源码如下:
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface GeneratedValue {
GenerationType strategy() default AUTO;
String generator() default "";
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="payablemoney_seq")
@SequenceGenerator(name="payablemoney_seq", sequenceName="seq_payment")
使用的具体例子为:
假设有一个表示人脸图片集合的类为FaceSet,其主键为facesetToken,则表示如下
@Entity(name = "face_set")
public class FaceSet{
}
- Sequence
@SequenceGenerator 中的 sequenceName 为序列的名称,需要与@GeneratedValue 中的generator对应
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="faceset_generator")
@SequenceGenerator(name="faceset_generator", sequenceName="faceset_seq")
@Column(name = "faceset_token", unique = true)
private String facesetToken;
- Auto
如果不指定具体的生成规则,则默认为AUTO,即下列两种情况等价
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String facesetToken;
@Id
private String facesetToken;
二、Hibernate主键策略生成
hibernate-5.0.12.Final版本的默认工厂中有14种生成策略,具体可见org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory
public DefaultIdentifierGeneratorFactory() {
register( "uuid2", UUIDGenerator.class );
register( "guid", GUIDGenerator.class ); // can be done with UUIDGenerator + strategy
register( "uuid", UUIDHexGenerator.class ); // "deprecated" for new use
register( "uuid.hex", UUIDHexGenerator.class ); // uuid.hex is deprecated
register( "assigned", Assigned.class );
register( "identity", IdentityGenerator.class );
register( "select", SelectGenerator.class );
register( "sequence", SequenceStyleGenerator.class );
register( "seqhilo", SequenceHiLoGenerator.class );
register( "increment", IncrementGenerator.class );
register( "foreign", ForeignGenerator.class );
register( "sequence-identity", SequenceIdentityGenerator.class );
register( "enhanced-sequence", SequenceStyleGenerator.class );
register( "enhanced-table", TableGenerator.class );
}
public void register(String strategy, Class generatorClass) {
LOG.debugf( "Registering IdentifierGenerator strategy [%s] -> [%s]", strategy, generatorClass.getName() );
final Class previous = generatorStrategyToClassNameMap.put( strategy, generatorClass );
if ( previous != null ) {
LOG.debugf( " - overriding [%s]", previous.getName() );
}
}
对几种比较常用的类型进行说明:
- uuid
采用128位的uuid算法生成主键,uuid被编码为一个32位16进制数字的字符串。
当使用strategy为uuid时,使用的时hibernate自己定义的UUID生成算法,此策略已过时,其具体实现参照org.hibernate.id. UUIDHexGenerator, 生成的字符串如402880876359adeb016359ae27190000
当使用strategy为uuid2时,此为此版本推荐使用的uuid生成算法,其默认采用标准的生成策略StandardRandomStrategy,实现为使用jdk自带的uuid生成方法,生成的字符串如
4af17c8e-8317-43e9-aff9-12d5590a71c6
@Id
@GeneratedValue(generator = "faceset_generator")
@GenericGenerator(name = "faceset_generator", strategy = "uuid")
- assigned
插入主键时,由程序来指定。相当于JPA中的AUTO。
@Id
@GeneratedValue(generator = "faceset_generator")
@GenericGenerator(name = "faceset_generator", strategy = "assigned")
- sequence
@Id
@GeneratedValue(generator = "faceset_generator")
@GenericGenerator(name = "faceset_generator", strategy = "sequence",
parameters = { @Parameter(name = "sequence", value = "faceset_seq") })
- guid
采用数据库底层的guid算法机制,对应MYSQL的uuid()函数,SQL Server的newid()函数,ORACLE的rawtohex(sys_guid())函数等
三、通过@GenericGenerator自定义主键生成策略
常用数据库支持生成规则如下:
数据库 | 支持的策略 |
---|---|
Postgres | GenerationType.TABLE GenerationType.AUTO GenerationType.IDENTITY GenerationType.SEQUENCE |
Oracle | GenerationType.TABLE GenerationType.AUTO GenerationType.SEQUENCE 不支持GenerationType.IDENTITY |
Mysql | GenerationType.TABLE GenerationType.AUTO GenerationType.IDENTITY 不支持GenerationType.SEQUENCE |