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. |