An optional sub-element is the name of a Java class, which is used to generate a unique identifier for the persistence class instance. If the generator instance requires some configuration values or initialization parameters, use
Element. For example:
Uid_table Next_hi_value_column |
All generators implement the org. hibernate. Id. identifiergenerator interface. This is a very simple interface; some applications can choose to provide their own specific implementations. Of course, Hibernate provides many built-in implementations. The following are the quick names of some built-in generators:
L Increment
Used to generate a unique identifier for long, short, or Int type. Data can be used only when no other process inserts data into the same table. Do not use it in a cluster.
Features: It is maintained by Hibernate and is suitable for all databases. It is not suitable for multi-process concurrent database updates and is suitable for accessing databases by a single process. Cannot be used in a cluster environment.
L identity
Supports built-in identity fields for DB2, MySQL, ms SQL Server, Sybase, and hypersonicsql. The returned identifier is of the long, short, or Int type.
Features: it is related to the underlying database and requires that the database support identity, for example, auto_increment in MySQL and identity in SQL Server. Supported databases include MySQL, SQL Server, DB2, Sybase, and hypersonicsql. Identity does not require Hibernate and user interference. It is easy to use, but it is not convenient to transplant programs between different databases.
L sequence
Use sequence in DB2, PostgreSQL, Oracle, sap db, and mckoi, and generator in InterBase ). The returned identifier is of the long, short, or Int type.
Features: the sequence of the underlying database is required. The databases supporting the sequence include DB2, PostgreSQL, qracle, sapdb, and other Porting Programs between different databases, the configuration file needs to be modified from the database supporting the sequence to the database that does not support the sequence.
L HiLo
Use a high/low 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 identifier generated by the high/low algorithm is unique only in a specific database.
Features: Additional database tables are required to ensure the uniqueness of primary keys in the same database, but they cannot guarantee the uniqueness of primary keys between multiple databases. The HiLo primary key generation mode is maintained by hibernate. Therefore, the Hilo mode has nothing to do with the underlying database. However, you should not manually modify the values of the tables used by the HI/LO algorithm. Otherwise, duplicate primary keys may occur.
L seqhilo
Use a high/low level algorithm to efficiently generate long, short, or Int type identifiers and give a database sequence name.
L UUID
Use a 128-bit UUID algorithm to generate string-type identifiers, which are unique in a network (using IP addresses ). UUID is encoded as a 32-bit hexadecimal number string.
Features; Ensure the uniqueness of the primary key in the database, and the generated primary key occupies a large amount of storage space
L guid
Use the guid string generated by the database in ms SQL Server and MySQL.
L native
Select one of identity, sequence, or HiLo Based on the capabilities of the underlying database.
Features: different underlying databases use different primary key generation methods. Hibernate uses different ing methods based on the underlying database, so it is easy to port the program. This method can be used if multiple databases are used in the project.
L assigned
Assign an identifier to the object before saving. This is the default generation policy for elements that are not scheduled.
Feature: the value generated for the primary key is completely determined by the user and has nothing to do with the underlying database. You need to maintain the primary key value. You must specify the primary key value before calling session. Save.
L select
You can use a database trigger to select rows with unique primary keys and return primary key values to allocate a primary key.
L foreign
Use the identifier of another associated object. Usually used together.