ID Primary Key Generation Policy of Hibernate

Source: Internet
Author: User

ID Primary Key Generation Policy of Hibernate
ID Generation Policy (1) implement automatic ID generation through XML configuration (test uuid and native)

Previously, we discussed that apart from creating a persistent bean through annotation, we can also create an hbm with the same name as bean under the package path of the bean to be persisted. xml file to implement the same function! For example:
User. hbm. xml file Configuration:

You can specify To automatically generate IDS:
Examples/y0ru49rHqyrbJ + rPJxvejrNPD09rJ + rPJ1ve8/examples + samples/samples + 3cr9vt2/4rXEsrvNrL2ru + samples = "http://www.2cto.com/uploadfile/Collfiles/20141009/20141009082052145.png" alt = "\">
Test. You do not need to set a value for the primary key ID:
View table creation statements and database data:


Experiment (use of native ):
The primary key generated using native is a number. In this case, the User id attribute should be of the int type.

View table creation statements and database data:

ID Generation Policy (2)

Automatic ID generation through annotation Configuration

By adding @ GeneratedValue annotation to the primary key field, you can also specify the Automatic Generation Policy of the primary key.
View the annotation source code. The default value of this annotation is AUTO. Its function is equivalent to native in xml configuration.

View table creation statements and database data :


View the source code. The @ GeneratedValue annotation has three other values:

AUTO-can be of the identity column type, sequence type, or table type, depending on different underlying databases. TABLE-use the TABLE to save the id value. IDENTITY-identity column: Specifies the database that supports IDENTITY. SEQUENCE databases, such as MYSQL, are not supported.

SEQUENCE-sequence specifies the database that supports SEQUENCE, and does not support the database that uses IDENTITY, such as ORACLE.
By default, different tables use the same SEQUENCE: hibernate_sequence when using SEQUENCE. If we need to specify a separate sequence for the table, we need to use annotation @ SequenceGenerator!
The name attribute is the name of the sequence generator, and the sequenceName is the name of the generated sequence.

Then we can specify the primary key in the object class to use this generator to generate the sequence:

Generate a primary key using annotation @ TableGenerator (cross-database platform ):
This annotation is used to generate an intermediate table to create a primary key for other business tables. When the business table obtains a primary key, it creates a new primary key for retrieval.
Name: name of the generator.
Table: name of the generated table (id_create_table). This table contains the field (table_name, table_id), table_name as the key value of the key-value pair, and the value of pkColumnValue (EVENT ), allocationSize specifies the auto-increment of the primary key by 1;

Usage:




ID generation policy (3)

Implement joint primary key through XML configuration

Sometimes we need to combine two or more fields of an object as the primary key, that is, the values of these fields cannot be the same for two or more objects.
Now we need to combine the id and name fields of the Dream field as the primary key:
Fields used as the primary key must be put into an entity separately. The entity must implement java. io. Serializable and override the equals and hashCode methods.
Dream references primary key entities:
XML configuration joint primary key:

Run:



Implement joint primary keys through annotation configuration (3 methods)
Annotation of the component class as @ Embeddable, and Annotation of the Component Property as @ Id:
The property annotation of the component is @ EmbeddedId:
Annotation of the class as @ IdClass, and Annotation of all attributes belonging to the primary key in the object as @ Id:

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.