Identifier generator of hibernate ID

Source: Internet
Author: User

"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. This method may cause problems: it cannot be used in a cluster. (This function can be used only when no other process inserts data into the same table.
)
"Identity"
Use the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL.
"Sequence"
Use the sequence mechanism provided by the database to generate the primary key. For example, sequence in oralce.
"Native" (local)
Hibernate uses identity, Hilo, and sequence as the primary key generation method based on the database used. "UUID. HEX"
Hibernate uses the 128-bit UUID algorithm to generate a hexadecimal value (represented by a 32-Bit String After encoding) as the primary key. "UUID. String"
Similar to UUID. HEX, the generated primary key is not encoded (Length: 16) and cannot be used in the PostgreSQL database.
"Foreign"
Use the identifier of another associated object as the primary key.

The following example:
1. Specify parameters:
<ID name = "ID" unsaved-value = "0">
<Generator class = "sequence">
<Param name = "sequence"> seq_child </param>
</Generator>
</ID>
Sequence is used and is suitable for Oracle databases;

2. Use the following methods in the configuration file to add fields to the database in SQL Server2000:
<ID name = "ID" type = "long" unsaved-value = "0">
<Column name = "ID" SQL-type = "numeric" not-null = "true"/>
<Generator class = "Identity"/>
</ID>
Here is mainly: Identity: indicates that the SQL Server2000 Database provides the sub-increment field by itself. If you want hibernate to provide it by yourself, use the increment keyword to implement it.
3. If the primary key in the table is of the string type: You can use the method provided by hibernate to realize the unique primary key:
<ID name = "ID" type = "string" unsaved-value = "null">
<Column name = "CID" SQL-type = "char (32)" not-null = "true"/>
<Generator class = "UUID. Hex"/>
</ID>
UUID. HEX: uses the 128-bit algorithm to generate a 32-Bit String. The most common method. Applicable to all databases.

* In the advanced section, implement your own primary key generator (ID generator) in hibernate)
Http://www.javaeye.com/topic/119240

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.