Primary key generation strategy for Hibernate framework

Source: Internet
Author: User
Tags unique id

In hibernate, the ID element's <generator> child element is used to generate a unique identifier for the object that persisted the class, which is the primary key. The hibernate framework defines a number of primary key generation policy classes, also known as generator classes. All generator classes implement the interface. Create your own generator class by implementing the Identifiergenerator interface. The Hibernate framework provides a number of built-in generator classes:

    • Assigned
    • Increment
    • Sequence
    • Hilo
    • Native
    • Identity
    • Seqhilo
    • Uuid
    • Guid
    • Select
    • Foreign
    • Sequence-identity

1, assigned

If the <generator> element is not used, assigned is the default generator policy. In this case, the application assigns an ID to the object.

2, Increment

When no other process inserts data into this table, it generates a unique ID. It generates a short,int or long identifier. The first generated identifier is typically 1, and then incremented to 1 at a time.

3., Sequencce

It uses a sequential sequence of databases. If no sequence is defined, it automatically creates a sequence. In the case of an Oracle database, it will create a sequence named Hibernate_sequence. In the case of DB2, SAP DB, Postgre SQL, or Mckoi, it uses a sequence (sequence), but uses the generator in InterBase. ,<generator> elements when using sequence type Chinese herbs use the <param> child element to specify the sequence name.

........    <id...>        <generator class= "sequence" >                <param name= "sequence" >h_test</param>        </generator>    </id>, ...


It uses high-low algorithms to generate IDs for Short,int and long types.

5, native

It uses identity, sequence, or Hilo depending on the database vendor.

6, Indentity

It is used for Sybase, Mysql, MS SQL Server, DB2, and hypersonic SQL support ID columns. The returned ID type is short, int, or long.

7, Seqhilo

It uses the high-low algorithm on the specified sequence name. The returned ID types are short, int, and long.


It uses the 128-bit UUID algorithm to generate the ID. The returned ID is of type string and is unique across the network (because IP is used). The UUID is identified in hexadecimal digits with a length of 32.

9. Guid

It uses a GUID generated by a database of type string. It is suitable for MS SQL Server and MySQL.

10. Select

It uses a database trigger to return a primary key.

11, Foreign

It uses the ID of another associated object, primarily for one-to-one correlation.

12, Sequence-identity

It uses a special sequence generation strategy. Supported only in Oracle 10g drivers.

Note: The high-low algorithm of Hilo and Seqhilo generation strategy is the HI/LO algorithm. Hi: High value------The value obtained from the database; LO: Low value------hibernate auto-Maintenance, value 1 to Max_low;max_low: The value that is configured in the mapping file.


Primary key generation strategy for Hibernate framework

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