程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate annoation (三 id生成器)

hibernate annoation (三 id生成器)

編輯:關於JAVA

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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved