Follow me: Learn hibernate in simple terms 4: Primary Key Generation Policy

Source: Internet
Author: User
Basic Principle of ORM ing

1. Mutual ing is implemented based on the same entity classes and tables. class objects correspond to records in tables, and different objects correspond to different records. Different records correspond to different objects.

2. Different records in the table are distinguished by the primary key. Different objects are distinguished by the Object ID. the Object ID is a member variable in the object. The value of this variable uniquely identifies the object.

3. The ing between object ID and table primary key forms the core of ORM.

4. databases often have specific primary key generation algorithms, but the object system does not. Therefore, you need to configure specific policies so that the object can have the object identification for data synchronization with the database.

Configure a primary key generation policy

1. The primary key generation policy is implemented through the configuration file.

2. The <ID> label in the ing file corresponds to the primary key column in the table.

3. <generator> Configure the primary key generation policy for sub-tags

4. The class attribute value specifies a specific policy, which can include the following identity, sequence, Hilo, native, swqhilo, increment, UUID, assigned, foreign, select

Primary Key Generation Policy format

<ID name = "ID" column = "Table primary key field name" type = "Java. Lang. Integer">

<Generator class = "Set primary key generation policy type"/>

</ID>

Common primary key generation methods
1. Assigned

In assigned mode, the program generates the primary key value and must specify it before saving (). Otherwise, an exception is thrown.

Feature: the value generated for the primary key is completely determined by the user and has nothing to do with the underlying database. You need to maintain the primary key value. You must specify the primary key value before calling session. Save.

2. HiLo

HiLo uses the high/low bit algorithm to generate a primary key. The high/low bit algorithm uses a high value and a low value, and then Concatenates the two values obtained by the algorithm as the unique primary key in the database. The HiLo method requires additional database tables and fields to provide the high value source. The table used by default is

Hibernate_unique_key. The default field is next_hi. Next_hi must have a record or an error may occur.

Features: Additional database tables are required to ensure the uniqueness of primary keys in the same database, but they cannot guarantee the uniqueness of primary keys between multiple databases. The HiLo primary key generation mode is maintained by hibernate. Therefore, the Hilo mode has nothing to do with the underlying database. However, you should not manually modify the values of the tables used by the HI/LO algorithm. Otherwise, duplicate primary keys may occur.

3. Increment

The increment method automatically increases the primary key value to generate a new primary key value, but the underlying database must support sequence. Such as Oracle and DB2. You must add the increment flag settings to the ing file XXX. HBM. xml.

Features: It is maintained by Hibernate and is suitable for all databases. It is not suitable for multi-process concurrent database updates and is suitable for accessing databases by a single process. Cannot be used in a cluster environment.

4. Identity

Identity supports automatic growth based on the underlying database. Different databases use different primary key growth methods.

Features: it is related to the underlying database and requires that the database support identity, for example, auto_increment in MySQL and identity in SQL Server. Supported databases include MySQL, SQL Server, DB2, Sybase, and hypersonicsql. Identity does not require Hibernate and user interference. It is easy to use, but it is not convenient to transplant programs between different databases.

5. Sequence

Sequence requires that the underlying database support the sequence method, such as the Oracle database.

Features: the sequence of the underlying database is required. The databases supporting the sequence include DB2, PostgreSQL, qracle, sapdb, and other Porting Programs between different databases, the configuration file needs to be modified from the database supporting the sequence to the database that does not support the sequence.

6. Native

Native primary key generation method automatically selects the identity, sequence, and HiLo primary key generation method based on different underlying databases.

Features: different underlying databases use different primary key generation methods. Hibernate uses different ing methods based on the underlying database, so it is easy to port the program. This method can be used if multiple databases are used in the project.

7. UUID

UUID uses the 128-bit UUID algorithm to generate a primary key. This ensures the uniqueness of the primary key in the network environment and the uniqueness of the primary key in different databases and servers.

Features; Ensure the uniqueness of the primary key in the database, and the generated primary key occupies a large amount of storage space

8. Foreign

Foreign is used in one-to-one relationships to ensure the uniqueness of the generated primary key. It supports SQL Server and MySQL.

Summary of Data Dependencies

1. UUID, increment, Hilo, and assigned are not dependent on databases.

2. Identity: Depends on MySQL or SQL Server. The primary key value cannot be maintained by hibernate.

3. sequence: Suitable for DBMS supporting sequences such as Oracle. The primary key value is not maintained by Hibernate and is generated by sequences.

4. Native: select an appropriate primary key generation policy based on the specific features of the underlying database. For MySQL or sqlserver, Select Identity. For Oracle, select sequence.

Summary

The main way to improve new capabilities of the system is to significantly reduce the number of database accesses. The above analysis can be used as an indicator for consideration. Using UUID to generate a hibernate primary key will provide the best performance and database platform adaptability.

We are actively talking about what policies are used to generate primary keys in our project, what are their advantages and disadvantages, and how they are handled, which brings benefits to the project.

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.