标签:属性 exception red tps order processor package 属性表 new
https://blog.csdn.net/u012838207/article/details/80406716
一、JPA通用策略生成器
通过annotation来映射hibernate实体的,基于annotation的hibernate主键标识为@Id,
其生成规则由@GeneratedValue设定的.这里的@id和@GeneratedValue都是JPA的标准用法,
JPA提供四种标准用法,由@GeneratedValue的源代码可以明显看出.
1
2
3
4
5
6
|
Target({METHOD,FIELD}) @Retention (RUNTIME) public @interface GeneratedValue{ GenerationType strategy() default AUTO; String generator() default "" ; } |
其中GenerationType:
1
2
3
4
5
6
|
public enum GenerationType{ TABLE, SEQUENCE, IDENTITY, AUTO } |
JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
TABLE比较复杂,这里不讲解。分别介绍其他三个:
1.SEQUENCE
实体类中的注解
1
2
3
|
@Id @GeneratedValue (strategy =GenerationType.SEQUENCE,generator= "aaa" ) @SequenceGenerator (name= "aaa" , sequenceName= "seq_payment" ) |
@SequenceGenerator定义
1
2
3
4
5
6
7
8
|
@Target ({TYPE, METHOD, FIELD}) @Retention (RUNTIME) public @interface SequenceGenerator { String name(); String sequenceName() default "" ; int initialValue() default 0 ; int allocationSize() default 50 ; } |
2.IDENTITY
主键则由数据库自动维护,使用起来很简单
1
2
|
@Id @GeneratedValue (strategy = GenerationType.IDENTITY) |
3、AUTO
默认的配置。如果不指定主键生成策略,默认为AUTO。
1
2
|
@Id @GeneratedValue (strategy = GenerationType.AUTO) |
二、hibernate主键策略生成器
hibernate提供多种主键生成策略,有点是类似于JPA,有的是hibernate特有,下面列出几个Hibernate比较常用的生成策略:
hibernate提供了多种生成器供选择,基于Annotation的方式通过@GenericGenerator实现.
hibernate每种主键生成策略提供接口org.hibernate.id.IdentifierGenerator的实现类,如果要实现自定义的主键生成策略也必须实现此接口.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public interface IdentifierGenerator { /** * The configuration parameter holding the entity name */ public static final String ENTITY_NAME = "entity_name" ; /** * Generate a new identifier. * @param session * @param object the entity or toplevel collection for which the id is being generated * * @return a new identifier * @throws HibernateException */ public Serializable generate(SessionImplementor session, Object object) throws HibernateException; } |
IdentifierGenerator提供一generate方法,generate方法返回产生的主键.
三、@GenericGenerator
自定义主键生成策略,由@GenericGenerator实现。
hibernate在JPA的基础上进行了扩展,可以用一下方式引入hibernate独有的主键生成策略,就是通过@GenericGenerator加入的。
比如说,JPA标准用法
1
2
|
@Id @GeneratedValue (GenerationType.AUTO) |
就可以用hibernate特有以下用法来代替:
1
2
3
|
@Id @GeneratedValue (generator = "paymentableGenerator" ) @GenericGenerator (name = "paymentableGenerator" , strategy = "assigned" ) |
@GenericGenerator的定义:
1
2
|
@Target ({PACKAGE, TYPE, METHOD, FIELD}) @Retention (RUNTIME) |
源代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public @interface GenericGenerator { /** * unique generator name */ String name(); /** * Generator strategy either a predefined Hibernate * strategy or a fully qualified class name. */ String strategy(); /** * Optional generator parameters */ Parameter[] parameters() default {}; } |
对于这些hibernate主键生成策略和各自的具体生成器之间的关系,在org.hibernate.id.IdentifierGeneratorFactory中指定了,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
static { GENERATORS.put( "uuid" , UUIDHexGenerator. class ); GENERATORS.put( "hilo" , TableHiLoGenerator. class ); GENERATORS.put( "assigned" , Assigned. class ); GENERATORS.put( "identity" , IdentityGenerator. class ); GENERATORS.put( "select" , SelectGenerator. class ); GENERATORS.put( "sequence" , SequenceGenerator. class ); GENERATORS.put( "seqhilo" , SequenceHiLoGenerator. class ); GENERATORS.put( "increment" , IncrementGenerator. class ); GENERATORS.put( "foreign" , ForeignGenerator. class ); GENERATORS.put( "guid" , GUIDGenerator. class ); GENERATORS.put( "uuid.hex" , UUIDHexGenerator. class ); //uuid.hex is deprecated GENERATORS.put( "sequence-identity" , SequenceIdentityGenerator. class ); } |
上面十二种策略,加上native,hibernate一共默认支持十三种生成策略。使用hibernate注解示例如下:
1
2
3
|
@Id @GeneratedValue (generator = "IDGenerator" ) @GenericGenerator (name = "IDGenerator" , strategy = "identity" ) |
这种完全类似于:
1
2
|
@Id @GeneratedValue (strategy=GenerationType.IDENTITY) |
标签:属性 exception red tps order processor package 属性表 new
原文地址:https://www.cnblogs.com/zouhong/p/11632126.html