基于按annotation的 hibernate主键生成策略

自定义主键生成策略,由@GenericGenerator实现。
hibernate在JPA的基础上进行了扩展,可以用一下方式引入hibernate独有的主键生成策略,就是通过@GenericGenerator 加入的。

比如说,JPA标准用法

Java代码
  1. @Id  
  2. @GeneratedValue(GenerationType.AUTO)  
@Id
@GeneratedValue(GenerationType.AUTO)


就可以用hibernate特有以下用法来实现

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "assigned")  
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "assigned")



@GenericGenerator的定义:

Java代码
  1. @Target({PACKAGE, TYPE, METHOD, FIELD})   
  2. @Retention(RUNTIME)   
  3. public @interface GenericGenerator {   
  4.   
  5. String name();   
  6.   
  7. String strategy();   
  8.   
  9. Parameter[] parameters() default {};   
  10. }  
@Target({PACKAGE, TYPE, METHOD, FIELD})
@Retention(RUNTIME)
public @interface GenericGenerator {
 
 String name();
 
 String strategy();
 
 Parameter[] parameters() default {};
}



name属性指定生成器名称。
strategy属性指定具体生成器的类名。
parameters得到strategy指定的具体生成器所用到的参数。

对于这些hibernate主键生成策略和各自的具体生成器之间的关系,在 org.hibernate.id.IdentifierGeneratorFactory中指定了,

Java代码
  1. static {   
  2.    GENERATORS.put("uuid", UUIDHexGenerator.class);   
  3.    GENERATORS.put("hilo", TableHiLoGenerator.class);   
  4.    GENERATORS.put("assigned", Assigned.class);   
  5.    GENERATORS.put("identity", IdentityGenerator.class);   
  6.    GENERATORS.put("select", SelectGenerator.class);   
  7.    GENERATORS.put("sequence", SequenceGenerator.class);   
  8.    GENERATORS.put("seqhilo", SequenceHiLoGenerator.class);   
  9.    GENERATORS.put("increment", IncrementGenerator.class);   
  10.    GENERATORS.put("foreign", ForeignGenerator.class);   
  11.    GENERATORS.put("guid", GUIDGenerator.class);   
  12.    GENERATORS.put("uuid.hex", UUIDHexGenerator.class); //uuid.hex is deprecated   
  13.    GENERATORS.put("sequence-identity", SequenceIdentityGenerator.class);   
  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一共默认支持十三种生成策略。

1、native

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "native")   
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "native")


2、uuid

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")   
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")


3、hilo

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "hilo")   
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "hilo")


4、assigned

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "assigned")   
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "assigned")


5、identity

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "identity")   
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "identity")


6、select

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")   
  2. @GenericGenerator(name="select", strategy="select",   
  3.       parameters = { @Parameter(name = "key", value = "idstoerung") })  
@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name="select", strategy="select",
     parameters = { @Parameter(name = "key", value = "idstoerung") })


7、sequence

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")   
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "sequence",   
  3.           parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })  
@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "sequence", 
         parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })


8、seqhilo

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")   
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo",   
  3.           parameters = { @Parameter(name = "max_lo", value = "5") })  
@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo", 
         parameters = { @Parameter(name = "max_lo", value = "5") })


9、increment

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "increment")   
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "increment")


10、foreign

Java代码
  1. @GeneratedValue(generator = "idGenerator")   
  2. @GenericGenerator(name = "idGenerator", strategy = "foreign",   
  3.           parameters = { @Parameter(name = "property", value = "employee") })  
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator", strategy = "foreign", 
         parameters = { @Parameter(name = "property", value = "employee") })



注意:直接使用@PrimaryKeyJoinColumn 报错(?)

Java代码
  1. @OneToOne(cascade = CascadeType.ALL)   
  2. @PrimaryKeyJoinColumn   
@OneToOne(cascade = CascadeType.ALL) 
@PrimaryKeyJoinColumn

例如

Java代码
  1. @Entity  
  2. public class Employee {   
  3.   @Id Integer id;   
  4.        
  5.   @OneToOne @PrimaryKeyJoinColumn  
  6.    EmployeeInfo info;   
  7.    ...   
  8. }  
@Entity
public class Employee {
  @Id Integer id;
    
  @OneToOne @PrimaryKeyJoinColumn
  EmployeeInfo info;
  ...
}

应该为

Java代码
  1. @Entity  
  2. public class Employee {   
  3.   @Id   
  4.   @GeneratedValue(generator = "idGenerator")   
  5.   @GenericGenerator(name = "idGenerator", strategy = "foreign",   
  6.           parameters = { @Parameter(name = "property", value = "info") })   
  7.    Integer id;   
  8.        
  9.   @OneToOne  
  10.    EmployeeInfo info;   
  11.    ...   
  12. }  
@Entity
public class Employee {
  @Id 
  @GeneratedValue(generator = "idGenerator")
  @GenericGenerator(name = "idGenerator", strategy = "foreign", 
         parameters = { @Parameter(name = "property", value = "info") }) 
  Integer id;
    
  @OneToOne
  EmployeeInfo info;
  ...
}

11、guid

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "guid")   
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "guid")


12、uuid.hex

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")     
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "uuid.hex")   
@GeneratedValue(generator = "paymentableGenerator")  
@GenericGenerator(name = "paymentableGenerator", strategy = "uuid.hex")


13、sequence-identity

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")   
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "sequence-identity",   
  3.           parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })  
@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "sequence-identity", 
         parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })


四、通过@GenericGenerator自定义主键生成策略
如果实际应用中,主键策略为程序指定了就用程序指定的主键(assigned),没有指定就从sequence中取。
明显上面所讨论的策略都不满足,只好自己扩展了,集成assigned和sequence两种策略。

Java代码
  1. public class AssignedSequenceGenerator extends SequenceGenerator implements   
  2. PersistentIdentifierGenerator, Configurable {   
  3. private String entityName;   
  4.      
  5. public void configure(Type type, Properties params, Dialect dialect) throws MappingException {   
  6.    entityName = params.getProperty(ENTITY_NAME);   
  7.   if (entityName==null) {   
  8.    throw new MappingException("no entity name");   
  9.    }   
  10.      
  11.   super.configure(type, params, dialect);     
  12. }   
  13.   
  14. public Serializable generate(SessionImplementor session, Object obj)   
  15.   throws HibernateException {   
  16.      
  17.    Serializable id = session.getEntityPersister( entityName, obj )   
  18.      .getIdentifier( obj, session.getEntityMode() );   
  19.      
  20.   if (id==null) {   
  21.     id = super.generate(session, obj);   
  22.    }   
  23.      
  24.   return id;   
  25. }   
  26. }  
public class AssignedSequenceGenerator extends SequenceGenerator implements 
 PersistentIdentifierGenerator, Configurable {
 private String entityName;
  
 public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
  entityName = params.getProperty(ENTITY_NAME);
  if (entityName==null) {
   throw new MappingException("no entity name");
  }
  
  super.configure(type, params, dialect);  
 }
 
 public Serializable generate(SessionImplementor session, Object obj) 
  throws HibernateException {
  
  Serializable id = session.getEntityPersister( entityName, obj ) 
    .getIdentifier( obj, session.getEntityMode() );
  
  if (id==null) {
   id = super.generate(session, obj);
  }
  
  return id;
 }
}


实际应用中,定义同sequence。

Java代码
  1. @GeneratedValue(generator = "paymentableGenerator")   
  2. @GenericGenerator(name = "paymentableGenerator", strategy = "AssignedSequenceGenerator",   
  3.       parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })  
@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "AssignedSequenceGenerator", 
     parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })


你可能感兴趣的:(java,Hibernate,jpa)