使用序列,向emp表插入记录,empno字段使用序列值
insert into emp(empno) values(emp_empno_seq.nextval);
insert into emp(empno) values(emp_empno_seq.nextval);
insert into emp(empno) values(emp_empno_seq.nextval);
修改emp_empno_seq序列的increment by属性为20,默认start with是1,alter sequence 序列名
alter sequence emp_empno_seq
increment by 20;
修改修改emp_empno_seq序列的的increment by属性为5
alter sequence emp_empno_seq
increment by 5;
修改emp_empno_seq序列的start with属性,行吗
alter sequence emp_empno_seq
start with 100;
不行,会报错
但是可以在创建序列的时候 ,指定起始值和增长值
有了序列后,还能为主健手工设置值吗?
insert into emp(empno) values(9999);
insert into emp(empno) values(7900);
可以
(1)是否需要底层数据库支持
identity需要底层数据库支持auto_increment。在MySQL数据库中,需要设置表的主键字段为自增长。
而increment和uuid不需要底层数据库支持、不需要设置主键字段为自增长。
(2)是否支持多线程并发操作?
increment只能单线程访问,多线程不行。
identity和uuid都支持并发操作。
(3)适用场景
如果只使用(专用)oracle数据库,可以使用sequence,Hibernate会自动在Oracle数据库中创建一个序列。
如果不确定使用oracle、mysql、sqlserver,可以使用native,它是一个通用的变量。一般都会使用native。
Hibernate帮助文档 Various additional generators
All generators implement the interface org.hibernate.id.IdentifierGenerator. This is a very simple interface. Some applications can choose to provide their own specialized implementations, however, Hibernate provides a range of built-in implementations. The shortcut names for the built-in generators are as follows:
increment
generates>long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
identity
supports>long, short or int.
sequence
uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned>long, short or int
uuid
Generates a 128-bit UUID based on a custom algorithm. The value generated is represented as a string of 32 hexidecimal digits. Users can also configure it to use a separator (config parameter "separator") which separates the hexidecimal digits into 8{sep}8{sep}4{sep}8{sep}4.
uuid2
Generates a IETF RFC 4122 compliant (variant 2) 128-bit UUID. The exact "version" (the RFC term) generated depends on the pluggable "generation strategy" used. Capable of generating values as java.util.UUID, java.lang.String or as a byte array of length 16 (byte[16]). The "generation strategy" is defined by the interface org.hibernate.id.UUIDGenerationStrategy.
guid
uses a database-generated GUID string on MS SQL Server and MySQL.
native
selects identity,sequence or hilo depending upon the capabilities of the underlying database.
assigned
lets the application assign an>save() is called. This is the default strategy if no element is specified.