The ID generation strategy for MySQL in hibernate

Source: Internet
Author: User
Tags uuid

Database planning and operation number encyclopedia, we usually set up a long tail keyword mining tool primary key.

The primary key can be divided into natural primary key and acting primary key.

The natural primary key indicates that a field with a transactional logical meaning is selected as the primary key of the table. For example, in the User Information table, select the user's ID number as the primary key. But then, as the transaction logic changes, the primary key is likely to change. Suppose, for example, which day the ID number is upgraded to 19, 2 bits, that ....

Acting PRIMARY key: An artificially added field in a table that does not indicate any transaction logic and is used only to identify a row of data. For example, in the User Information table, add a field for the user ID. The record used to indicate the user's information.

In general, the use of the comparison is the method of the primary key. Also, we are used to making the primary key field active, to ensure that it has only sex. However, a different method of actively increasing the database is not the same. such as in SQL Server, with Identity,mysql, there is a general selection of increment,oracle sequence. In this way, the primary key column of the database operation, it will appear to compare the trouble.

In hibernate, however, a primary key generation strategy is provided. Here are a few of the more commonly used comparisons:

1:assigned

----indicates that the value of the primary key is specified by the application when new data is added. The primary key is to choose a natural primary key method. This method applies to primary key columns instead of actively adding columns.

The defect is that when performing a new operation, it is necessary to query the database to determine whether the primary key generated is now present.

2:increment

----indicates that the primary key value is actively generated by hibernate when new data operations are added. Its generated value is: First query the maximum value of the primary key column, and then add 1 on the basis of the maximum value. Applies to primary key columns that use the acting primary key method. The same table that cannot be used for primary key columns is actively incremented. However, the primary key generation strategy is also flawed.

(1) The new data before the first query, affecting the function.

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

(3) In the concurrency operation of the conflict of doubt.

3:identity

----is more of a custom-made Sqlerver database. Table that is primarily applicable to the active increment column for SQL Server databases.

4:native

----indicate that a different primary key generation strategy is chosen based on a different database. For example, when the database is SQL Server, the identity is chosen, for example, Oracle, the

Sequence in Oracle, and so on. A different database differs from the database dialect that is configured in Sessionfactory in the Hibernate master configuration file.

5.uuid

* Only the primary key generation method is available. Extract it from hibernate

Advantage: Avoid the generation of IDs, and the database once again dealings, the function is high. But with regard to some developers who are not very accustomed to this ID generation method, the UUID generates a string of 32, not the same as the identity from the 1 beginning integer.

Using Hibernate's primary key generation strategy, you can compare sensitive and convenient operations on the primary key fields in the table. And, not the same database, not the same as the primary key method, and only need to modify the next mapping file can be

Related Article

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.