疯狂

STANDING ON THE SHOULDERS OF GIANTS
posts - 481, comments - 486, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

hibernate annoation (三 id生成器)

Posted on 2009-11-02 14:57 疯狂 阅读(3391) 评论(0)  编辑  收藏 所属分类: java hibernate

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

使用@GeneratedValue
1 正对不同的数据库可以同时使用
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
2 针对mysql
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
3 针对oracle
       @Id
       @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="s_gen")
       @SequenceGenerator(name="s_gen",sequenceName="s_seq")
说明:@GeneratedValue()的strategy属性支持5中id生成器:除上面3中外还有GenerationType.TABLE
2配合使用@GenericGenerator 不单独使用
hibernate内不可使用的id生成器可见代码
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 );
)
如果想要不同的表使用相同的主键生成器,可以把他的generator的name属性设为相同即可
例如:
@GeneratedValue(name="id1")
@GenericGenerator(name="id1",strategy="identity")

 

1、native

 @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "native")

2、uuid
@GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")

3、hilo

@GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "hilo")

4、assigned
@GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "assigned")

5、identity

@GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "identity")

6、select

@GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name="select", strategy="select", parameters = { @Parameter(name = "key", value = "idstoerung") })

7、sequence

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

8、seqhilo

  @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo", parameters = { @Parameter(name = "max_lo", value = "5") })

9、increment

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

 


只有注册用户登录后才能发表评论。


网站导航: