java高级我爱编程Dao

SpringDataJpa-主键生成策略

2018-05-17  本文已影响76人  小菜鸟托比

一、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{
}
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="faceset_generator")
  @SequenceGenerator(name="faceset_generator", sequenceName="faceset_seq")
  @Column(name = "faceset_token", unique = true)
  private String facesetToken;
@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() );
        }
    }

对几种比较常用的类型进行说明:

@Id
@GeneratedValue(generator = "faceset_generator")
@GenericGenerator(name = "faceset_generator", strategy = "uuid")
@Id
@GeneratedValue(generator = "faceset_generator")
@GenericGenerator(name = "faceset_generator", strategy = "assigned")

@Id
@GeneratedValue(generator = "faceset_generator")  
@GenericGenerator(name = "faceset_generator", strategy = "sequence",   
         parameters = { @Parameter(name = "sequence", value = "faceset_seq") }) 

三、通过@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
上一篇下一篇

猜你喜欢

热点阅读