资源描述:
《基于Hibernate Anotation的ID生成策略》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、基于按annotation的hibernate主键生成策略文章分类:Java编程基于按annotation的hibernate主键生成策略自定义主键生成策略,由@GenericGenerator实现。hibernate在JPA的基础上进行了扩展,可以用一下方式引入hibernate独有的主键生成策略,就是通过@GenericGenerator加入的。比如说,JPA标准用法Java代码1.@Id 2.@GeneratedValue(GenerationType.AUTO) @Id@GeneratedValue(GenerationType.AUTO)
2、就可以用hibernate特有以下用法来实现Java代码1.@GeneratedValue(generator="paymentableGenerator") 2.@GenericGenerator(name="paymentableGenerator",strategy="assigned") @GeneratedValue(generator="paymentableGenerator")@GenericGenerator(name="paymentableGenerator",strategy="assigned")@GenericGe
3、nerator的定义:Java代码1.@Target({PACKAGE,TYPE,METHOD,FIELD}) 2.@Retention(RUNTIME) 3.public@interfaceGenericGenerator{ 4. 5.Stringname(); 6. 7.Stringstrategy(); 8. 9.Parameter[]parameters()default{}; 10.} @Target({PACKAGE,TYPE,METHOD,FIELD})@Retention(RUNTIME)public@interface
4、GenericGenerator{Stringname();Stringstrategy();Parameter[]parameters()default{};}name属性指定生成器名称。strategy属性指定具体生成器的类名。parameters得到strategy指定的具体生成器所用到的参数。对于这些hibernate主键生成策略和各自的具体生成器之间的关系,在org.hibernate.id.IdentifierGeneratorFactory中指定了,Java代码1.static{ 2. GENERATORS.put("uuid",
5、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",SequenceG
6、enerator.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",UUI
7、DHexGenerator.class);//uuid.hexisdeprecated 13. GENERATORS.put("sequence-identity",SequenceIdentityGenerator.class); 14.} static{GENERATORS.put("uuid",UUIDHexGenerator.class);GENERATORS.put("hilo",TableHiLoGenerator.class);GENERATORS.put("assigned",Assigned.class);GENERATOR
8、S.put("identity",IdentityGenerator.class);GENERATORS.put("sel