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")
介绍说明:@GeneratedValuestrategy属性支持5中id生成器:除上面3中外还有GenerationType.TABLE
2配合使用@GenericGenerator 不单独使用
hibernate内不可使用id生成器可见代码
GENERATORS.put( "uuid", UUIDHexGenerator. );
GENERATORS.put( "hilo", TableHiLoGenerator. );
GENERATORS.put( "assigned", Assigned. );
GENERATORS.put( "identity", IdentityGenerator. );
GENERATORS.put( "select", SelectGenerator. );
GENERATORS.put( "sequence", SequenceGenerator. );
GENERATORS.put( "seqhilo", SequenceHiLoGenerator. );
GENERATORS.put( "increment", IncrementGenerator. );
GENERATORS.put( "foreign", ForeignGenerator. );
GENERATORS.put( "guid", GUIDGenerator. );
GENERATORS.put( "uuid.hex", UUIDHexGenerator. ); // uuid.hex is deprecated
GENERATORS.put( "sequence-identity", SequenceIdentityGenerator. );
)
如果想要区别表使用相同主键生成器可以把他generatorname属性设为相同即可
例如:
@GeneratedValue(name="id1")
@GenericGenerator(name="id1",strategy="identity")
最新评论