Hibernate series notes (4)---primary key generation strategy

Source: Internet
Author: User
Tags uuid

Primary key generation Policy

Common build strategies are divided into six types

1, Increment   

The maximum value of the primary key is fetched from the database by hibernate (only 1 times per session), based on that value, each increment is 1, the primary key is generated in memory and is not dependent on the underlying database, so it can cross the database.

<id name= "id" column= "id" >

<generator class= "Increment"/>

</id>

Hibernate calls the Generate () method inside the Org.hibernate.id.IncrementGenerator class, using the Select Max (idcolumnname) from TableName statement to get the primary key maximum value. This method is declared as synchronized, so there is no problem inside a standalone Java virtual machine, however, the same value may be fetched when multiple JVMs concurrently access the database select Max, and then insert occurs dumplicate Entry's error. So there can only be one hibernate application process to access the database, otherwise it may produce primary key conflicts, so it is not suitable for multi-process concurrent update database, suitable for a single process to access the database, not for the cluster environment.

Official documents: Use only when no other process is inserting data into the same table, not in the cluster.

Features: cross-database, not suitable for multi-process concurrent update database, suitable for a single process to access the database, not for the cluster environment.

2.identity

The identity is generated by the underlying database identifier. The identity is generated by the database itself, but the primary key must be set to self-grow, using identity as a precondition for the underlying database to support autogrow field types such as DB2, SQL Server, MySQL, Sybase, and Hypersonicsql, etc. Oracle does not support this type of field without self-increment.

<id name= "id" column= "id" >

<generator class= "Identity"/>

</id>

Example: If you use a MySQL database, the primary key field must be set to Auto_increment.

ID Int (one) primary key auto_increment

Features: Use only in field databases that support autogrow, such as MySQL. does not cause thread safety issues

3.sequence

Using the sequence mechanism provided by the database to generate the primary key requires the database to support sequence. Sequence in Oralce, DB, SAP db, Postgersql, Mckoi. MySQL does not support the sequence database (you can use identity).

<generator class= "Sequence" >

<param name= "sequence" >hibernate_id</param>

</generator>

<param name= "sequence" >hibernate_id</param> Specify the name of the sequence

When hibernate generates a primary key, it looks for sequence and assigns it to the primary key value, the primary key value is generated by the database, hibernate is not responsible for maintenance, and you must first create a sequence if you do not specify the sequence name. Use Hibernate's default sequence, named Hibernate_sequence, if you want to create the sequence in the database.

Features: can only be used in a database that supports sequences, such as Oracle.

4.native

Native by hibernate according to the database used by the use of identity, Hilo, sequence one of the primary key generation mode, flexibility is strong. If the identity is supported, the identity is used and sequence is used if sequence is supported.

<id name= "id" column= "id" >

<generator class= "native"/>

</id>

For example, MySQL uses identity,oracle to use sequence

Note: If hibernate automatically selects sequence or Hilo, all table primary keys will be taken from Hibernate's default sequence or Hilo table. Also, some databases are not very efficient in support of the default primary key generation test.

When using sequence or Hilo, you can add parameters, specify sequence names or Hi-value table names, and so on, such as

<param name= "sequence" >hibernate_id</param>

Features: According to the automatic selection of the database, if you use multiple databases in the project, you can use this method, you need to set the table's self-increment field or set up a sequence, establish a table, etc.

5. UUID

uuid:universally unique Identifier, refers to the number generated on a machine, which guarantees that all machines in the same time and space are unique. Based on the standard calculations developed by the Open Software Foundation (OSF), the Ethernet card address, nanosecond time, chip ID code and many possible numbers are used, the standard UUID format is:

Xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)

where each x is a hexadecimal number in the range of 0-9 or a-f.

<id name= "id" column= "id" >

<generator class= "uuid"/>

</id>

Hibernate creates a UUID string as the primary key when saving the object, but it does not have any business logic meaning, only as the primary key, the only drawback length is larger, 32 bits (hibernate will be the middle of the UUID "-" deleted) string, occupy large storage space, But there are two very important advantages, hibernate in maintaining the primary key, do not go to the database query, thereby improving efficiency, and it is a cross-database, and later switching the database is extremely convenient.

Features: UUID length, occupy large space, cross-database, without accessing the database to generate the primary key value, so high efficiency and can guarantee uniqueness, porting is very convenient, recommended use.

6, assigned

The primary key is generated by an external program and must be specified before save (). Hibernate is not responsible for maintaining primary key generation. It is independent of hibernate and the underlying database, and can be cross-database. Before storing the object, it is necessary to use the setter method of the primary key to assign a value to the primary key, and as to how this value is generated, it is entirely up to you to avoid this method.

<id name= "id" column= "id" >

<generator class= "Assigned"/>

</id>

"UD" is a custom policy name, with an artificially named name, followed by "UD".

Features: Can be cross-database, human control of primary key generation, should be avoided as far as possible.

I am here to talk about here, in more detail can be consulted: http://www.cnblogs.com/kakafra/archive/2012/09/16/2687569.html

Thank you!

Hibernate series notes (4)---primary key generation strategy

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.