Hibernate ID Generator

Source: Internet
Author: User

From: http://blog.163.com/wex660@126/blog/static/24153065200851842848236/

 

In hibernate, the optional <generator> sub-element under the <ID> label is the name of a Java class, which is used to generate a unique identifier for the persistence class instance. All generators implement net. SF. hibernate. id. identifiergenerator interface this is a very simple interface. Some applications can choose to provide their own specific implementations. Of course, Hibernate provides many built-in implementations. Below are some built-in primary key generators (key generator) meaning

1) assigned
The primary key is generated by an external program and does not involve hibernate. Assign an identifier to the object before saving. This is the default generation policy when the <generator> element is not specified.

2) HiLo
The primary key generation mechanism implemented by the HI/lo algorithm requires additional database tables to save the Historical Status of the primary key generation. Use a high/low algorithm to efficiently generate long, short, or Int type identifiers. Specify a table and field (hibernate_unique_key and next_hi by default) as the source of the high value. The identifier generated by the high/low algorithm is unique only in a specific database.

3) seqhilo
Similar to hilo, the primary key generation mechanism implemented through the HI/lo algorithm only stores the Historical Status of the primary key in sequence, which is suitable for databases that support sequence, such as oracle. Use a high/low level algorithm to efficiently generate long, short, or Int type identifiers and give a database sequence name.
4) Increment
The primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, then, when you need to generate a primary key, adding 1 as the primary key may cause the following problem: if multiple instances access the same database, because each instance maintains its own primary key status, different instances may generate the same primary key, resulting in duplicate primary key exceptions. Therefore, if the same database has multiple instances to access, this method must be avoided.
Used to generate a unique identifier for long, short, or Int type. Data can be used only when no other process inserts data into the same table. Do not use it in a cluster.
5) Identity
Use the primary key generation mechanism provided by the database, such as the primary key generation mechanism in DB2 SQL Server MySQL. Supports built-in identity fields for DB2, MySQL, ms SQL Server, Sybase, and hypersonicsql. The returned identifier is of the long, short, or Int type.

6) sequence
Use the sequence mechanism provided by the database to generate a primary key, such as the sequence in oralce. Use sequence in DB2, PostgreSQL, Oracle, sap db, and mckoi, and generator in InterBase ). The returned identifier is of the long, short, or Int type.

7) Native
Hibernate uses identity HiLo sequence as the primary key generation method based on the underlying database.

8) UUID. HEX
Use a 128-bit UUID algorithm to generate string-type identifiers, which are unique in a network (using IP addresses ). UUID is encoded as a 32-bit hexadecimal number string. Hibernate generates a hexadecimal value based on a 128-bit unique value (encoded as a 32-Bit String) as the primary key, for example, 5abefd3890cat33llsica3ee43bk222.
9) UUID. String
Similar to UUID. HEX, the generated primary key is not encoded (Length: 16). problems may occur in some databases (such as PostgreSQL)

10) guid

Use the guid string generated by the database in ms SQL Server and MySQL.

11) Select

You can use a database trigger to select rows with unique primary keys and return primary key values to allocate a primary key.

12) Foreign

Use the identifier of another associated object. Usually used together with <one-to-one>.

 
Generally, UUID is used. the hex primary key generation method provides the best performance and database platform adaptability. In addition, common databases, such as oracledb2sqlservermysql, provide an easy-to-use primary key generation mechanism (auto-increase field or sequence) we can use the generator-class = native primary key generation method in the primary key generation mechanism provided by the database. However, it is worth noting that the primary key generation mechanism provided by some databases is not necessarily the best in efficiency, A large number of concurrent insert data may cause the mutual lock between tables. The primary key generation mechanism provided by the database is usually used to save the current primary key status in an internal table (for example, for an auto-increment primary key, this internal table maintains the current maximum and incremental values). Then, the maximum value is read each time data is inserted, and the incremental value is added as the primary key of the new record, then, the new maximum value is updated back to the internal table. In this way, an insert operation may result in multiple internal table read/write operations in the database, along with data lock and unlock operations, this has a great impact on performance. Therefore, UUID is recommended for systems with high requirements for concurrent insert. hex as the primary key generation mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.