This article describes the meaning of Hibernate's generator attribute. The generator attribute has 7 classes, and this article briefly describes the meaning and usage of these 7 classes.
[XHTML]View Plaincopy
- <class name="Onlyfun.caterpillar.User"
- table="USER">
- <ID name="id" type="string" unsaved-value="null">
- <column name="user_id"/>
- <generator class="Uuid.hex"/>
- </ID>
- </class>
Hibernate's Generator attribute has 7 classes, this article briefly describes the meaning and usage of these 7 classes.
1, Identity: for MySQL database. Features: Increment
[XHTML]View Plaincopy
- <ID name="id" column= "id">
- < generator class="Identity"/>
- </ID>
Note: The primary key is specified as the Auto_increment property for the MySQL database when you need to build the table when using an incremental sequence.
2. Sequence: for Oracle database
[XHTML]View Plaincopy
- <ID name="id" column= "id">
- <generator class="sequence">
- <param name="sequence"> sequence name </param>
- </Generator>
- </ID>
3, native: cross-database use, generated by the underlying dialect.
Default.sequence to Hibernate_sequence
[XHTML]View Plaincopy
- <ID name="id" column= "id">
- <generator class="native"/>
- </ID>
Note: Hibernate will find the hibernate_sequence sequence in Oracle by default when using native.
If the sequence is not in Oracle, an error is encountered when the Oracle database is connected.
4, Hilo: Through the high-low-level synthesis ID, first build table Hi_value, and then build the column Next_value. Must have an initial value.
[XHTML]View Plaincopy
- <ID name="id" column= "id">
- <generator class="Hilo">
- <param name="table">high_val</param>
- <param name="column">nextval</param>
- <param name="Max_lo">5</param>
- </Generator>
- </ID>
5, Sequencehilo: With high-low synthesis ID, build a sequence sequence, do not build a table.
[C-sharp]View Plaincopy
- <id name="id" column="id" >
- <generator class="Hilo" >
- <param name="sequence" >high_val_seq</param>
- <param name="Max_lo" >5</param>
- </generator>
- </id>
6, Assigned: User custom ID;
[XHTML]View Plaincopy
- <ID name="id" column= "id">
- <generator class="Assigned"/>
- </ID>
7, Foreign: For one-to-one relationship sharing the main health, two ID values.
This article explains the quick names of some of the built-in generators for HBM's generator child elements in hibernate. The generator child element is a very simple interface, and some applications can choose to provide their own specific implementations.
The < generator> child element that must be declared in *.hbm.xml is the name of a Java class used to generate a unique identity for an instance of the persisted class.
[XHTML]View Plaincopy
- <generator class="sequence"/>
This is a very simple interface, and some applications can choose to provide their own specific implementations. Of course, Hibernate provides a lot of built-in implementations. Here are the quick names of some of the built-in generators for the generator child elements:
Increment (increment)
Used to generate a unique identity for a long, short, or int type. It can only be used if no other process is inserting data into the same table. Do not use under the cluster.
Identity
Provides support for built-in identity fields for Db2,mysql, MS SQL Server, Sybase, and Hypersonicsql. The returned identifiers are of type long, short, or int.
Sequence (sequence)
In Db2,postgresql, Oracle, SAP DB, Mckoi use sequences (sequence), and generators (generator) are used in InterBase. The returned identifiers are of type long, short, or int.
Hilo (High-low)
Use a high/low algorithm to efficiently generate identifiers for long, short, or int types. Given a table and a field (the default is Hibernate_unique_key and Next_hi respectively) as a high-level worthy source. The high/low algorithm generates identifiers that are unique only in a particular database. Do not use this generator for connections obtained using JTA or for connections provided by users themselves.
Seqhilo (using the high and low bits of the sequence)
Use a high/low algorithm to efficiently generate identifiers of long, short, or int types, given the name of a database sequence (sequence).
Uuid.hex
Generates an identifier for the string type with a 128-bit UUID algorithm. Unique in a network (IP address is used). The UUID is encoded as a string of 32-bit 16 decimal digits.
Uuid.string
Use the same UUID algorithm. The UUID is encoded as a string of any ASCII character that is 16 characters long. cannot be used in a PostgreSQL database
Native (local)
Choose identity, Sequence, or one of Hilo based on the ability of the underlying database.
Assigned (Program Settings)
Let the application assign an identifier to the object before save ().
Foreign (external reference)
Use an identifier for another associated object. Used in conjunction with the < one-to-one>.
Generator the use of child elements:
[XHTML]View Plaincopy
- <class name="Onlyfun.caterpillar.User" table="User">
- <ID name="id" type="string" unsaved-value="null">
- <column name="user_id"/>
- <generator class="Uuid.hex"/>
- </ID>
- </class>