Oracle Data uses the primary key generation policy

Source: Internet
Author: User
Hibernate is an open-source object relationship ing framework that encapsulates JDBC objects in a lightweight manner, allowing Java programmers to use object programming as they wish.

Hibernate is an open-source object relationship ing framework that encapsulates JDBC objects in a lightweight manner, allowing Java programmers to use object programming as they wish.

Hibernate is an open-source object relationship ing framework. It encapsulates JDBC objects in a lightweight manner, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate can be used in any situation where JDBC is used. It can be used in Java client programs or Servlet/JSP Web applications. The most revolutionary is that, hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence.

Assigned

In Assigned mode, the user generates the primary key value and must specify the value before saving (). Otherwise, Hibernate-related books will be discarded.

Exception: The generated value of the primary key is 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.

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. By default, the table used is hibernate_unique_key, and 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.

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.

Identity

Identity supports automatic growth based on the underlying database at that time. Different databases use different primary Hibernate related books.

Key growth mode. 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.

Sequence

Sequence requires the support of the underlying database in the Sequence mode, such as the Oracle database. Features: requires the support of the underlying database in Sequence, database supporting sequences include DB2, PostgreSql, Oracle, SAPDb, and other Porting Programs between different databases. In particular, You need to modify the configuration file from a database supporting sequences to a database that does not support sequences.

Native

Native primary key generation method automatically selects Identity, Sequence, and Hilo primary key generation methods 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.

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

Foreign GUID

Use the field of the External table as the primary key. Generally, using uuid. hex to generate a primary key provides the best performance and database platform adaptability.

This 10 method generates OID identifiers. increment is usually used to assign the power generated by the identifiers to Hibernate for processing. however, when multiple Hibernate applications operate on the same database or even the same table at the same time. we recommend that you use identity based on the underlying database, but the database must support automatic growth. Of course, you can choose different methods for different databases. if you are not sure what the database you are using supports. you can choose to use native to enable Hibernate to help you select identity, sequence, or hilo.

In addition, common databases such as Oracle, DB2, SQL Server, and MySql provide easy-to-use primary key generation mechanisms (Auto-Increase fields or Sequence ). We can use the primary key generation method of generator-class = native 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 may not be optimal in terms of efficiency, and a large number of concurrent insert data may cause inter-Table locks. The primary key generation mechanism provided by the database is usually to save the current primary key status in an internal table (for example, for an auto-incrementing 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, for systems with high requirements for concurrent Insert, uuid. hex is recommended 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.