Hibernate's Generator property

Source: Internet
Author: User

      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
  1. <class name="Onlyfun.caterpillar.User"
  2. table="USER">
  3. <ID name="id" type="string" unsaved-value="null">
  4. <column name="user_id"/>
  5. <generator class="Uuid.hex"/>
  6. </ID>
  7. </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
    1. <ID name="id" column= "id">
    2. < generator class="Identity"/>
    3. </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
  1. <ID name="id" column= "id">
  2. <generator class="sequence">
  3. <param name="sequence"> sequence name </param>
  4. </Generator>
  5. </ID>

3, native: cross-database use, generated by the underlying dialect.
Default.sequence to Hibernate_sequence

[XHTML]View Plaincopy
    1. <ID name="id" column= "id">
    2. <generator class="native"/>
    3. </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
  1. <ID name="id" column= "id">
  2. <generator class="Hilo">
  3. <param name="table">high_val</param>
  4. <param name="column">nextval</param>
  5. <param name="Max_lo">5</param>
  6. </Generator>
  7. </ID>

5, Sequencehilo: With high-low synthesis ID, build a sequence sequence, do not build a table.

[C-sharp]View Plaincopy
    1. <id name="id" column="id" >
    2. <generator class="Hilo" >
    3. <param name="sequence" >high_val_seq</param>
    4. <param name="Max_lo" >5</param>
    5. </generator>
    6. </id>

6, Assigned: User custom ID;

[XHTML]View Plaincopy
    1. <ID name="id" column= "id">
    2. <generator class="Assigned"/>
    3. </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
    1. <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
  1. <class name="Onlyfun.caterpillar.User" table="User">
  2. <ID name="id" type="string" unsaved-value="null">
  3. <column name="user_id"/>
  4. <generator class="Uuid.hex"/>
  5. </ID>
  6. </class>

Hibernate's Generator property

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.