Significance of the generator attribute of Hibernate

Source: Internet
Author: User
    Http://www.cnblogs.com/talo/articles/1663978.html
    This article describes the significance of the generator attribute of hibernate. There are 7 types of generator attributes. This article briefly describes the meaning and usage of these 7 classes.
    •    
         
      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>  

     

    The generator attribute of hibernate has 7 classes. This article briefly describes the meaning and usage of these 7 classes.

    1. Identity: used for MySQL databases. Features: Incremental

     
     
    1. < id name="id" column="id">
    2.     < generator class="identity"/>
    3. < /id>

    Note: When using an incremental sequence for a MySQL database, you must specify the primary key as the auto_increment attribute when creating a table.

    2. sequence: used for Oracle databases

     
     
    1. < IDName = "ID" column = "ID">
    2. < GeneratorClass = "sequence">
    3. < ParamName = "sequence">Sequence name</Param>
    4. </Generator>
    5. </ID>

    3. Native: used across databases, produced by underlying dialects.
    Default. sequence is hibernate_sequence

     
     
    1. < id name="id" column="id">
    2.    < generator class="native"/>
    3. < /id>

    Note: When native is used, Hibernate searches for the hibernate_sequence sequence in Oracle by default.
    If the sequence does not exist in Oracle, an error is returned when you connect to the Oracle database.

    4. HiLo: Create the hi_value table and create the next_value column by merging the IDs of the high and low bits. An initial value is required.

     
     
    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: The same as the high and low merging ID. Create a sequence without table creation.

     
     
    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-Defined ID;

     
     
    1. < id name="id" column="id">
    2. < generator class="assigned"/>
    3. < /id>

    7. Foreign: used for one-to-one relationship to share the master key, the two IDs are the same.

    This article describes the quick names of some built-in generators of the generator sub-elements of HBM in hibernate. The generator sub-element is a very simple interface; some applications can choose to provide their own specific implementations.

    The <generator> sub-element that must be declared in *. HBM. XML is a Java class name, which is used to generate a unique identifier for the persistence class instance.

    <Generator class = "sequence"/>

    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 of the generator subelement:

    Increment (incremental)

    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.

    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.

    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.

    HiLo (high/low)

    Use a high/low level 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 high value. The identifier generated by the high/low algorithm is unique only in a specific database. Do not use this generator in the connection obtained by using JTA or the connection provided by the user.

    Seqhilo (using the high and low bits of the sequence)

    Use a high/low level algorithm to efficiently generate long, short, or Int type identifiers and give a database sequence name.

    UUID. HEX

    Use a 128-bit UUID algorithm to generate string-type identifiers. It is unique in a network (using an IP address ). UUID is encoded as a 32-bit hexadecimal number string.

    UUID. String

    Use the same UUID algorithm. UUID is encoded as a string consisting of 16 characters long and any ASCII characters. Cannot be used in a PostgreSQL database

    Native (local)

    Select one of identity, sequence, or HiLo Based on the capabilities of the underlying database.

    Assigned (program settings)

    Assign an identifier to the object before saving.

    Foreign (External Reference)

    Use the identifier of another associated object. And <one-to-one>.

    Usage of the generator sub-element:

    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.