JPA实体注解与hibernate主键生成策略

JPA实体注解与hibernate主键生成策略

<wbr><wbr>用hibernate注解开发项目,对于主键的生成策略有些模糊,下面是从新浪网里面看到的一篇关于hibernate注解以及主键生成策略的文章,值得一看:</wbr></wbr>

<wbr><wbr></wbr></wbr>

1. 实体标志:

<wbr><wbr><wbr></wbr></wbr></wbr>@Entity
<wbr><wbr><wbr>@Indexed(index="group")<br><wbr><wbr><wbr>@Table(name="GROUP_LAYMOD")</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>* @Indexed 标识需要进行索引的对象,<br><wbr><wbr><wbr>* 属性 : index 指定索引文件的路径 @DocumentId 用于标示实体类中的唯一的属性保存在索引文件中,是当进行全文检索时可以这个唯一的属性来区分索引中其他实体对象,一般使用实体类中的主键属性<br><wbr><wbr><wbr>* @Field 标注在类的get属性上,标识一个索引的Field<br><wbr><wbr><wbr><wbr><wbr>属性 : index 指定是否索引,与Lucene相同<br><wbr><wbr><wbr><wbr><wbr>store 指定是否索引,与Lucene相同<br><wbr><wbr><wbr><wbr><wbr>name 指定Field的name,默认为类属性的名称<br><wbr><wbr><wbr><wbr><wbr>analyzer 指定分析器</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

在hibernate注解主键的时候,一般会使用到这两个
<wbr><wbr>@GeneratedValue的作用是JPA的默认实现自定义主键生成策略<br><wbr><wbr>@GenericGenerator是hibernate在JPA的基础上增强。</wbr></wbr></wbr></wbr>

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

比如说,JPA标准用法

@Id<wbr><wbr><br> @GeneratedValue(GenerationType.AUTO)<wbr><wbr><br><br> 就可以用hibernate特有以下用法来实现<br><br><strong>@GeneratedValue(generator<wbr>=<wbr>"paymentableGenerator")<wbr><wbr><wbr><wbr><wbr><br> @GenericGenerator(name<wbr>=<wbr>"paymentableGenerator",<wbr>strategy<wbr>=<wbr>"assigned")<wbr><wbr><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></strong><br><br> @GenericGenerator的定义:<br><br> @Target({PACKAGE,<wbr>TYPE,<wbr>METHOD,<wbr>FIELD})<wbr><wbr><wbr><br> @Retention(RUNTIME)<wbr><wbr><wbr><br> public<wbr>@interface<wbr>GenericGenerator<wbr>{<wbr><wbr><wbr><br><wbr><wbr><br> String<wbr>name();<wbr><wbr><wbr><br><wbr><wbr><br> String<wbr>strategy();<wbr><wbr><wbr><br><wbr><wbr><br> Parameter[]<wbr>parameters()<wbr>default<wbr>{};<wbr><wbr><wbr><br> }<wbr><wbr><br><br><br> name属性指定生成器名称。<br> strategy属性指定具体生成器的类名。<br> parameters得到strategy指定的具体生成器所用到的参数。<br><br> 对于这些hibernate主键生成策略和各自的具体生成器之间的关系,在org.hibernate.id.IdentifierGeneratorFacto<wbr>ry中指定了,<br><br> static<wbr>{<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("uuid",<wbr>UUIDHexGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("hilo",<wbr>TableHiLoGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("assigned",<wbr>Assigned.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("identity",<wbr>IdentityGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("select",<wbr>SelectGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("sequence",<wbr>SequenceGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("seqhilo",<wbr>SequenceHiLoGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("increment",<wbr>IncrementGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("foreign",<wbr>ForeignGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("guid",<wbr>GUIDGenerator.class);<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("uuid.hex",<wbr>UUIDHexGenerator.class);<wbr>//uuid.hex<wbr>is<wbr>deprecated<wbr><wbr><wbr><br><wbr><wbr><wbr>GENERATORS.put("sequence-identity",<wbr>SequenceIdentityGenerato<wbr>r.class);<wbr><wbr><wbr><br> }<wbr><wbr><br><br> 上面十二种策略,加上native,hibernate一共默认支持十三种生成策略。<br><br> 1、native<br> Java代码<br> @GeneratedValue(generator<wbr>=<wbr>"paymentableGenerator")<wbr><wbr><wbr><wbr><wbr><br> @GenericGenerator(name<wbr>=<wbr>"paymentableGenerator",<wbr>strategy<wbr>=<wbr>"native")<wbr><wbr><wbr><br><br> 2、uuid<br> Java代码<br> @GeneratedValue(generator<wbr>=<wbr>"paymentableGenerator")<wbr><wbr><wbr><wbr><wbr><br> @GenericGenerator(name<wbr>=<wbr>"paymentableGenerator",<wbr>strategy<wbr>=<wbr>"uuid")<wbr><wbr><wbr><br><br> 3、hilo<br> Java代码<br> @GeneratedValue(generator<wbr>=<wbr>"paymentableGenerator")<wbr><wbr><wbr><wbr><wbr><br> @GenericGenerator(name<wbr>=<wbr>"paymentableGenerator",<wbr>strategy<wbr>=<wbr>"hilo")<wbr><wbr><wbr><br><br> 4、assigned<br> Java代码<br> @GeneratedValue(generator<wbr>=<wbr>"paymentableGenerator")<wbr><wbr><wbr><wbr><wbr><br> @GenericGenerator(name<wbr>=<wbr>"paymentableGenerator",<wbr>strategy<wbr>=<wbr>"assigned")<wbr><wbr><wbr><br><br> 5、identity<br> Java代码<br> @GeneratedValue(generator<wbr>=<wbr>"paymentableGenerator")<wbr><wbr><wbr><wbr><wbr><br> @GenericGenerator(name<wbr>=<wbr>"paymentableGenerator",<wbr>strategy<wbr>=<wbr>"identity")<wbr><wbr><wbr><br><br> 6、select<br> Java代码<br> @GeneratedValue(generator<wbr>=<wbr>"paymentableGenerator")<wbr><wbr><wbr><br> @GenericGenerator(name="select",<wbr>strategy="select",<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>parameters<wbr>=<wbr>{<wbr>@Parameter(name<wbr>=<wbr>"key",<wbr>value<wbr>=<wbr>"idstoerung")<wbr>})<wbr><wbr><br><br> 7、sequence<br> Java代码<br> @GeneratedValue(generator<wbr>=<wbr>"paymentableGenerator")<wbr><wbr><wbr><br> @GenericGenerator(name<wbr>=<wbr>"paymentableGenerator",<wbr>strategy<wbr>=<wbr>"sequence",<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(Hibernate)