Common Hibernate primary key generation strategy

Source: Internet
Author: User

Today, I learned about the Hibernate primary key generation strategy problem, summed up, the deficiencies, please point out.

Hibernate is representative of the excellent persistence layer framework. In traditional Jdbc+javabean operations, the entity objects are encapsulated by the programmer and then returned. In Hibernate, the object-relational mapping "ORM" greatly simplifies the operation of the database.

In the design and operation of the database, we usually create a primary key for the table. Primary keys, which can be divided into natural primary keys and surrogate primary keys.

A natural primary key indicates that a field with business logic meaning is used as the primary key of the table. For example, in the User Information table, using the user's ID number as the primary key. However, as the business logic changes, the primary key is likely to change. For example, suppose that the ID card number is upgraded to 19, 2, that ....

Surrogate PRIMARY key: Artificially adding a field to the table that does not represent any business logic, just to identify a row of data. For example, in the User Information table, add a field for the user ID. The record used to represent the user's information.

Typically, more often than not, the form of a surrogate primary key is used. Moreover, we are accustomed to allowing the primary key field to grow automatically to ensure its uniqueness. However, the way different databases grow automatically is not the same. In SQL Server, in the case of Identity,mysql, increment,oracle is usually used in sequence. As a result, the primary key column of the database operation, it will appear more cumbersome.

However, in Hibernate, a hibernate primary key generation strategy is provided. Here are some of the more commonly used:

1:assigned

Represents the value of a primary key that is specified by the application when new data is added. Mainly for the primary key is the use of natural primary key form. In this way, the primary key column is not an automatic growth column.

The disadvantage is that when you perform a new operation, you need to query the database to determine whether the generated primary key already exists.

2:increment

Indicates that the primary key value is automatically generated by hibernate when a new data operation is made. The value generated is: First query the maximum value of the primary key column, then add 1 to the maximum value. Applies to primary key columns in the form of surrogate primary keys. The same cannot be used for primary key columns that are automatically grown tables. However, the primary key generation strategy also has some drawbacks.

(1) The new data before the query once, affecting the performance.

(2) The type of the primary key can only be a numeric int or a long

(3) Concurrent operation of the conflict problem.

3:identity

Rather, it is tailor-made for the Sqlerver database. Table that is primarily applicable to the automatic growth column of the SQL Server database.

4:native

Represents the use of different Hibernate primary key generation strategies based on different databases. For example, if the current database is SQL Server, identity is used, as with Oracle,

Sequence in Oracle. The difference between databases is to hibernate the database dialect configured in Sessionfactory in the primary configuration file.

XML code

<id name="实体类属性名" type="java.lang.Integer">
       <column name="对应表中主键字段名" />
       <generator class="assiged|increment|identity|native|........" />
</id>

With the Hibernate primary key generation strategy, it is more flexible and convenient to manipulate the primary key fields in the table. Moreover, different databases, different primary key forms, and only need to modify the mapping file on it.

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.