Hibernate3.2 (9): built-in primary key generator of Hibernate

Source: Internet
Author: User

Generally, Hibernate recommends defining an identifier attribute for a persistence class to uniquely identify a persistence instance.The identity attribute must be mapped to the primary key of the underlying data table.

Identifier PropertyPass<ID.../>The <ID.../> elementName attributeThe value is the identifier property name of the persistence class. In addition, the <ID.../> element can specify the following optional attributes:

Optional attribute of the <ID.../> element
Attribute name Description
Name If the name attribute is not set, it indicates that the persistence class has no identity attribute. If this parameter is set, the value of name is of the persistence class.
Identifies the property name.
Type Specifies the Data Type of the Identity attribute. This type can be either a built-in hibernate type or a Java type. If you use
For Java type, you must use a fully qualified class name. This attribute is optional. If this attribute is not specified in the ing file, Hibernate determines the Data Type of this identity attribute. We recommend that you set this attribute to ensure better performance.
Column Sets the column name of the data column mapped to the identity property. By default, the column name is the same as the attribute name of the Identity attribute.
Unsaved-Value Specifies the value of the Identity attribute when an instance has just been created and has not been saved. This attribute value can be used to distinguish an instance from an instance that has been loaded from a previous session but has not been persisted. In hibernate3, you do not need to set this attribute.
Access Specifies the access policy for hibernate to access this identity attribute. The default value is property. This attribute overwrites the default-access attribute in the root element

Almost all modern database modeling theories recommend that you do not use physical primary keys of practical significance, but instead use logical primary keys without any practical significance. Avoid using complex physical primary keys whenever possible. consider adding a column to the database as the logical primary key. The logical primary key has no practical significance and is only used to identify a row of records. Hibernate providesPrimary Key GeneratorWhich generates a unique logical primary key value for each persistent instance.

The primary key generator is used to generate the primary key of the data table record. Generally, there are the following common primary key generators:

Common primary key generators
Name Description
Increment Generates a unique identifier for a long, short, or Int primary key. Data can be used only when no other process inserts data into the same table. Do not use it in a cluster.
Identity Applicable to tables that support identity (auto-increment) Primary keys, such as DB2, MySQL, SQL Server, Sybase, and hypersonicsql. The returned identity attribute is of the long, short, or Int type.
Sequence Applicable to data tables that support sequence, such as DB2, PostgreSQL, Oracle, sap db, and mckoi. The returned identity property value is of the long, short, or Int type.
HiLo Use a high/low bit algorithm to efficiently generate long, short, or Int type identifiers. Specify a table and field (hibernate_unique_key and next_hi by default) as the source of the high value. The ID attribute value generated by the high/low bit algorithm is unique only in a specific database.
Seqhilo To use a high/low bit algorithm to efficiently generate long, short, or Int type identifiers, you must specify a database sequence name. This algorithm is slightly different from Hilo. It stores the Historical Status of the primary key in sequence and is suitable for databases that support sequence, such as oracle.
UUID Use a 128-bit UUID algorithm to generate string-type identifiers, which are unique in a network (IP addresses are also used as algorithm data sources ). UUID is encoded into a 32-bit hexadecimal string.
Guid Use the guid string generated by the database in SQL Server and MySQL.
Native Select one of identity, sequence, or HiLo Based on the capabilities of the underlying database.
Assigned Assign an identifier to the object before saving. This is equivalent to the Default policy used when no <generator.../> element is specified.
Select Use a database trigger to select a row with a unique primary key and return its primary key value as the identification attribute value.
Foreign Indicates that the identity property value of another associated object is used directly. This type of primary key generator is only useful in ing between 1 and 1 based on the primary key.

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.