hibernate加jpa自带一共14种。
1.native
@Id @GeneratedValue(generator = "gen") @GenericGenerator(name = "gen", strategy = "native")
或
@Id @GeneratedValue(strategy=GenerationType.AUTO)
2.table : org.hibernate.id.enhanced.TableGenerator
@Id @GeneratedValue(strategy=GenerationType.TABLE, generator="empGen") @TableGenerator( name="empGen", table="ID_GEN", pkColumnName="GEN_KEY", valueColumnName="GEN_VALUE", pkColumnValue="EMP_ID", allocationSize=1) public int id;
3.uuid : org.hibernate.id.UUIDHexGenerator
最高生成36位长唯一字符串(加上分隔符),默认32位
@Id @GeneratedValue(generator = "gen") @GenericGenerator( name = "gen", strategy = "uuid", parameters={ //@Parameter(name="separator",value="自定义分隔符,长度1位") }) private String id;
4.hilo : org.hibernate.id.TableHiLoGenerator
@Id @GeneratedValue(generator="gen") @GenericGenerator( name="gen", strategy="hilo", parameters={ //@Parameter(name="table",value="hibernate_unique_key"), //@Parameter(name="column",value="next_hi"), //@Parameter(name="schema",value="schema"), //@Parameter(name="catalog",value="catalog"), //@Parameter(name="max_lo",value="20") }) private int id;
注释部分可以做相应的修改
5.assigned : org.hibernate.id.Assigned
@Id private int id;
6.identity : org.hibernate.id.IdentityGenerator
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id;
7.select : org.hibernate.id.SelectGenerator
8.sequence : org.hibernate.id.SequenceGenerator
@Id @GeneratedValue(generator="gen") @GenericGenerator( name="gen", strategy="sequence", parameters={ //@Parameter(name="sequence",value="hibernate_sequence"), //@Parameter(name="parameters",value=""), //@Parameter(name="schema",value=""), //@Parameter(name="catalog",value="") }) private int id;
或
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE) private int id;
支持DB2.H2.HSQL.Informix.Ingres.Interbase.Mckoi.Mimer.Oracle.Postgre.RDMSOS2200.SAPDB.TimesTen
9.seqhilo : org.hibernate.id.SequenceHiLoGenerator
@Id @GeneratedValue(generator="gen") @GenericGenerator( name="gen", strategy="seqhilo", parameters={ //@Parameter(name="sequence",value="hibernate_sequence"), //@Parameter(name="parameters",value=""), //@Parameter(name="schema",value=""), //@Parameter(name="catalog",value=""), //@Parameter(name="max_lo",value="20") }) private int id;
支持DB2.H2.HSQL.Informix.Ingres.Interbase.Mckoi.Mimer.Oracle.Postgre.RDMSOS2200.SAPDB.TimesTen
10.increment : org.hibernate.id.IncrementGenerator
@Entity @Table(name = "id_increment_gen") public class IncrementGen { @Id @GeneratedValue(generator="gen") @GenericGenerator( name="gen", strategy="increment", parameters={ @Parameter(name="tables",value="id_increment_gen"), //@Parameter(name="identity_tables",value="id_increment_gen"), @Parameter(name="column",value="id"), //@Parameter(name="target_column",value="id"), //@Parameter(name="schema",value=""), //@Parameter(name="catalog",value="") }) private int id;其中tables参数和identity_tables参数互斥,tables参数优先。column参数和target_column参数互斥, column参数优先,支持多表,以","分隔,会生成类似如下的语句:
select max (ids_.id) from ( select id from id_increment_gen ) ids_多表时为:
select max (ids_.id) from ( select id from id_increment_gen1 union select id from id_increment_gen2 ) ids_
11.foreign : org.hibernate.id. ForeignGenerator
12.guid : org.hibernate.id. GUIDGenerator
@Id @GeneratedValue(generator = "gen") @GenericGenerator(name = "gen", strategy = "guid") private String id;
支持mysql,oracle,sybase
13.uuid.hex : org.hibernate.id.UUIDHexGenerator // uuid.hex is deprecated
14.sequence-identity : org.hibernate.id.SequenceIdentityGenerator
15.自定义
可同时实现接口org.hibernate.id.IdentifierGenerator,org.hibernate.id.Configurable
@Id @GeneratedValue(generator = "gen") @GenericGenerator(name = "gen", strategy = "xg.CustGenerator") private String id;
strategy填写具体类名