Hibernate ID primary key generation policy

Source: Internet
Author: User

In the design and operation of the database, we usually establish a primary key for the table.
Primary key, which can be divided into natural primary key and proxy primary key.
Natural primary key means: Use a field with business logic meaning 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 business logic changes, the primary key is likely to change. For example, suppose one day the ID number is upgraded to 19, 2 bits, that ....
Surrogate PRIMARY Key: An artificially added field in a table that does not represent any business logic and is used only to identify a single row of data. For example, in the User Information table, add a User ID field. The record used to represent the user's information.

In general, the more commonly used is the form of the surrogate primary key. Furthermore, we are accustomed to allowing the primary key field to grow automatically to ensure its uniqueness. However, the way that different databases are automatically grown is not the same. As in SQL Server, sequence is usually used in Identity,mysql, which is used in increment,oracle. In this way, the primary key column of the database operation, it will appear more cumbersome.
In hibernate, however, a primary key generation strategy is provided. Here are some of the more common:
1:assigned
----represents the value of the primary key specified by the application when new data is added. The primary key is primarily the form of a natural primary key. In this way, the primary key column is not the autogrow column.

The disadvantage is that when you perform a new operation, you need to query the database to determine whether the primary key that was generated already exists.
2:increment
----indicates that the primary key value is automatically generated by hibernate when new data operations are added. The resulting value is: Query the maximum value of the primary key column first, and then add 1 on top of the maximum value. Applies to primary key columns in the form of a proxy primary key. The same cannot be used for tables where the primary key column is autogrow. However, the primary key generation strategy also has some drawbacks.
(1) The new data before the first query, affecting the performance.
(2) The type of the primary key can only be a numeric int or a long
(3) Conflicting problems in concurrency operation.
3:identity
----is more of a custom-made Sqlerver database. Tables that are primarily applicable to the autogrow column of SQL Server databases.
4:native
----means that different primary key generation strategies are used depending on the database. For example, if the current database is SQL Server, the identity will be used, such as Oracle, the
Sequence in Oracle, and so on. The distinction between databases differs from the database dialect that is configured in Sessionfactory in the Hibernate master configuration file.

5.uuid
* Unique Primary key generation method. Extract it from hibernate

Pros: Avoid the generation of IDs, and the database once again dealings, high performance. But for some developers who are not accustomed to this type of ID generation, the UUID generates a string of 32, different from the integer that the identity begins with 1.


XML code
1.<id Name= "Entity class Property name" Type= "Java.lang.Integer" >
2. <column name= "PRIMARY key field name in corresponding table"/>
3. <generator class= "assiged|increment|identity|native| ..."/>
4.</id>
<id Name= "Entity class Property name" Type= "Java.lang.Integer" >
<column name= "PRIMARY key field name in corresponding table"/>
<generator class= "assiged|increment|identity|native| ..."/>
</id>

Using Hibernate's primary key generation strategy, it is more flexible and convenient to manipulate the primary key fields in the table. Also, different databases, different primary key forms, only need to modify the next mapping file can be

Transfer from http://lqzit.iteye.com/blog/845163

Hibernate ID primary key generation policy

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.