hibernate annoation (三 id生成器)

id生成:

hibernate内不可使用的id生成器可见代码

使用@GeneratedValue

1 正对不同的数据库可以同时使用

  @Id @GeneratedValue(strategy = GenerationType.AUTO)

2 针对mysql

 @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

3 针对oracle

  @Id <p>@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="s_gen")</p> @SequenceGenerator(name="s_gen",sequenceName="s_seq")

说明:@GeneratedValue()的strategy属性支持5中id生成器:除上面3中外还有GenerationType.TABLE

2配合使用@GenericGenerator 不单独使用

hibernate内不可使用的id生成器可见代码

GENERATORS.put( "uuid", UUIDHexGenerator.class ); <p>GENERATORS.put( "hilo", TableHiLoGenerator.class );</p> <p>GENERATORS.put( "assigned", Assigned.class );</p> <p>GENERATORS.put( "identity", IdentityGenerator.class );</p> <p>GENERATORS.put( "select", SelectGenerator.class );</p> <p>GENERATORS.put( "sequence", SequenceGenerator.class );</p> <p>GENERATORS.put( "seqhilo", SequenceHiLoGenerator.class );</p> <p>GENERATORS.put( "increment", IncrementGenerator.class );</p> <p>GENERATORS.put( "foreign", ForeignGenerator.class );</p> <p>GENERATORS.put( "guid", GUIDGenerator.class );</p> <p>GENERATORS.put( "uuid.hex", UUIDHexGenerator.class ); // uuid.hex is deprecated</p> <p>GENERATORS.put( "sequence-identity", SequenceIdentityGenerator.class );</p> )

如果想要不同的表使用相同的主键生成器,可以把他的generator的name属性设为相同即可

例如:

@GeneratedValue(name="id1") <p>@GenericGenerator(name="id1",strategy="identity")

相关推荐