Hibernate primary key generation policy and hibernate primary key policy

Source: Internet
Author: User

Hibernate primary key generation policy and hibernate primary key policy
The <generator> sub-element in the ing file in hibernate is the name of a Java class, which is used to generate a unique identifier for the persistence class instance. If the generator instance requires some configuration values or initialization parameters, use the <param> element for transmission.

<Id name = "id" type = "Integer" column = "id">
<Generator class = "org. hibernate. id. TableHiLoGenerator">
<Param name = "table"> main_id </param>
<Param name = "column"> hi_value_column </param>
</Generator>
</Id>
All generators implement the org. 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.
The following are the quick names of some built-in generators:

1. 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, and then add 1 as the primary key each time the primary key needs to be generated. 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. This method may cause problems: it cannot be used in a cluster.

2. identity
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.

3. sequence
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.

4. hilo
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.

5. seqhilo
Use a high/low level algorithm to efficiently generate long, short, or int type identifiers and give a database sequence name.

6. uuid
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.

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

8. native
Select one of identity, sequence, or hilo Based on the capabilities of the underlying database.

9. assigned
Assign an identifier to the object before saving. This is the default generation policy when the <generator> element is not specified.

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

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

12. sequence-identity
A special sequence generation policy uses database sequences to generate actual values. However, it is combined with getGeneratedKeys of JDBC3 so that the generated values are returned when the insert statement is executed. Currently, only Oracle 10 Gb for JDK 1.4 is supported.

The driver supports this policy. Note that due to a bug in the Oracle driver, the comments of these insert statements are disabled. (Original article: Note comments on these insert statements are disabled due to a bug in the Oracle drivers .)




Related Article

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.