Induction of policies for generating hibernate primary keys

Source: Internet
Author: User

Hibernate is one of the excellent persistence layer frameworks. We all know that in the traditional JDBC + JavaBeanProgramDuring development, Entity objects are encapsulated by the programmer and then returned. In hibernate, Object-based relational ORM ing ORM (introduced in the previous blog) is used to not only implement operations on our commonly used relational databases with an object-oriented approach, it also greatly simplifies database operations.

In database design and operations, we can't do without creating a primary key for the table. Primary keys can be divided into natural primary keys and proxy primary keys, which respectively indicate the following meanings:

Natural primary key: Fields with business logic meanings are used as the table's primary keys. For example, in the user information table, the user's ID card number is used as the primary key. However, as the business logic changes, the primary key may be changed. For example, if the ID card number is upgraded to or 20 digits, you can imagine it...

Proxy primary key: Manually add a field to the table. This field does not represent any business logic and is only used to identify a row of data. For example, add a user ID field in the user information table. The record used to indicate the user information.

Generally, the proxy primary key is usually used. Furthermore, we are used to making the primary key field automatically grow to ensure its uniqueness. However, the automatic growth of different databases is not the same. For example, in sqlserver, there is an increment in identity and MySQL, and sequence is usually used in Oracle. In this way, operations on primary key columns in the database will be troublesome. But don't worry. In hibernate, we have provided a set of primary key generation policies. Below are some of the more common simple introductions. If you have special requirements, it is better to refer to the help documentation.

* Identity

This is an automatic growth type. It is applicable to MySQL, SQL Server, and DB2. It uses the primary key generated by the database to generate a unique identifier for long, short, and INT types.

KeyCodeDisplay:

MySQL: Create Table t_user (ID int auto_incrementprimary key, name varchar (20); SQL SERVER: Create Table t_user (ID int identity (1, 1) primary key, name varchar (20); <idname = "ID" column = "ID" type = "long"> <generator class = "Identity"/> </ID>

* Sequence

Sequence supported by DB2 and Oracle, used to generate a unique identifier for long, short, or int

The database syntax is as follows:

ORACLE: Create sequence seq_name increment by 1 start with 1;

When you need a primary key value, you can call seq_name.nextval or seq_name.curval to obtain it. The database will help us maintain this sequence and ensure that each retrieved value is unique, for example:

 
Insert into tbl_name (ID, name) values (seq_name.nextval, 'jimliu '); <ID name = "ID" column = "ID" type = "long"> <generatorclass = "sequence"> <paramname = "sequence"> seq_name </param> </Generator> </ID>

If the sequence parameter is not specified, Hibernate will access a default sequence, which is hibernate_sequence. We also need to create this sequence in the database.

* UUID

Uses a 128-bit UUIDAlgorithmGenerate a string type identifier. UUID is encoded into a 32-bit hexadecimal number string. UUID includes: IP address, JVM start time, system time (accurate to 1/4 seconds), and a counter value (unique in JVM)Hibernate calculates a 128-bit unique value insert.

 
<ID name = "ID" column = "ID"> <generator class = "UUID. Hex"/> </ID>

UUID. String

Hibernate calculates a 16-Bit Insert value.

* HiLo

Long, short, or Int type identifiers generated using a high/low level algorithm. A table and field are given as the source of the high value. The default table is hibernate_unique_key, and the default field is next_hi. It divides the ID source into two parts, DB + memory, and then generates the id value together according to the algorithm. Multiple records can be generated within a few connections, improving efficiency.

 
MySQL: Create Table hi_value (next_hi integer not null); insert into hi_value (next_hi) values (1 ); <ID name = "ID" column = "ID"> <generator class = "HiLo"> <Param name = "table"> hi_value </param> <Param name = "column "> next_hi </param> <Param name =" max_lo "> 100 </param> </generator> </ID>

During Hibernate Persistence, Hibernate is responsible for generating low-level values. When the Hilo identifier generator generates the identifier, it needs to retrieve the current value of next_hi from the hi_value table, and then modify the value. This operation is completed in a separate transaction. The maximum low value is configured in max_lo, but when the low value generated in hibernate memory exceeds this value, you need to read the high value again in the hi_value table of the database. To use the Hilo generation policy, create an additional table in the database. The default table name is hibernate_unique_key. The default field is of the integer type and the name is next_hi (rarely used)
You can also set the custom table name and field name.

 
<ID name = "ID" type = "integer"> <column name = "ID"/> <generator class = "HiLo"> <Param name = "my_unique_key"/> <param column = "next_hi"/> </generator> </ID>

* Native

Select one from identity, sequence, and HiLo Based on the capabilities of the underlying database, which is more flexible. However, if sequence or HiLo is selected, the primary keys of all tables are retrieved from the default sequence or HiLo table of hibernate. In addition, some databases support the test of primary key generation by default, and the efficiency is not very high.

For Oracle using sequence, For MySQL and SQL Server using identity (auto-incrementing primary key generation mechanism), native is to submit the primary key generation work to the database, Hibernate (very common)

 
<Idname = "ID" column = "ID"> <generator class = "native"/> </ID>

* Assigned

The application is responsible for generating Primary Key Identifiers. The primary key is usually used in the database where there is no proxy primary key. The primary key used is business-related, such:

<Idname = "ID" column = "ID" type = "string"> <generator class = "assigned"/> </ID>

This primary key generation method is not recommended. When designing a database table, you should use a proxy primary key (surrogate key) instead of a natural primary key (natural key has business meaning ), when the <generator> label is not specified, the assigned primary key is generated by default.When inserting data, the primary key is added by the user, and hibernate does not

* Foreign

Use External table fields as primary keys

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.