Hibernate provides built-in identifier generator

Source: Internet
Author: User

Hibernate provides built-in identifier generator

The Java language identifies or distinguishes different objects of the same class by memory address, and the relational database identifies or distinguishes different records of the same table by primary key. Hibernate uses the OID(object identifier) to unify the contradiction between them, and the OID is the equivalent of the primary key in the relational database (often referred to as the surrogate master) in the Java object model.

The ,<id> element in the object-relational mapping file is used to set the object identifier, for example:
        <id  Name= "id"  type= "java.lang.Integer";
             <column name= "ID" &NBSP;/>
           <generator class = "Identity" &NBSP;/>
         </id>
<generator> element is used to set the identity generator. Hibernate provides identifier generator interface:   Org.hibernate.id.IdentifierGenerator Interface, and provides a variety of built-in implementations. For example: &NBSP; org.hibernate.id.IdentityGenerator; Org.hibernate.id.IncrementGenerator ; Their abbreviated names are: Identity and increment. The following two methods are equivalent when you set the Class property of the <generator> child element to provide either the full identifier generator's name or the abbreviated name.

<id name= "id" type= "Java.lang.Integer" >

<column name= "ID"/>

<generator class= "Org.hibernate.id.IdentityGenerator"/>

</id>

Or:

<id name= "id" type= "Java.lang.Integer" >
<column name= "ID"/>
<generator Class= "Identity"/>
</id>

Identifier generator

say Ming

Increment

Applies to the proxy primary key. The identifier is automatically generated incrementally by hibernate, increasing by 1 at a time.
Advantage: Because its mechanism does not depend on the underlying database system, it is suitable for all database systems.
Disadvantage: It is only suitable for a single hibernate application process to access the same database, which is not recommended in a clustered environment. In addition, the OID must be of type long,int,short, and if it is a byte type, there will be an exception.

Identity

Applies to the proxy primary key. Generated by the underlying database identifier. The prerequisite is that the underlying database supports autogrow field types. (Oracle database cannot use it)

Sequence

Applies to the proxy primary key. Hibernate generates identifiers based on the sequence of the underlying database. The prerequisite is that the underlying database supports the sequence. (Oracle database can use it)

Hilo  

Applies to the proxy primary key. Hibernate generates identifiers based on the High/low algorithm . Hibernate takes the field of a particular table as the "high" value. The Next_hi field of the Hibernate_unique_key table is selected by default. Its mechanism is not dependent on the underlying database system, so it is suitable for all database systems. Identifiers generated by the High/low algorithm are guaranteed to be unique only in one database.

Native

Applies to the proxy primary key. Choose Identity,sequence, Hilo, based on the support capabilities of the underlying database for automatically generated identifiers. Ideal for cross-platform development, where the same hibernate application needs to connect to a variety of database systems.

Uuid.hex

Applies to the proxy primary key. Hibernate uses a 128-bit UUID algorithm to generate identifiers. The UUID algorithm can generate a unique string identifier in a network environment. This identifier generation strategy is not popular because the primary key of the string type takes up more database space than the primary key of the integer type.

Assigned

Applies to natural primary keys. The Java application is responsible for generating identifiers, in order to allow the Java application to set the OID, cannot declare the SetID () method as private type, should try to avoid using the natural primary key.

1 Proxy primary key:

 Note: does not have an obligation meaning, so it adapts to changing business requirements, which are usually integer types such as short, long, int, and their wrapper type.

2 Natural PRIMARY Key:

have business implications.

3 identifier generators that can be used in several commonly used database systems:

MYSQL:identity, Increment, Hiho, native

MS SQL Server :identity, Increment, Hiho, native

Oracle :sequence, identity, increment, Hiho, native

Cross-platform development:native

A few examples:

1 , if the primary key field is a self-increment type,
Then the XML declaration for the ID field in the. hbm.xml file corresponds to the
It should be written like this:
<generator class= "native"/>
For example:
<id
Column= "user_id"
Name= "Id"
Type= "Integer"
>
<generator class= "native"/>
</id>
In fact, this native is not the actual type, but hiberante according to
The currently used database, automatically using the corresponding type.
For example, if the sqlserver,native corresponds to the identity
See Hiberante Reference:
Native (local)
Choose identity, Sequence, or one of Hilo based on the ability of the underlying database.

2 , if the primary key field is not set to self-increment, but int type of,
You can use increment to generate a primary key from Hibernate.
<generator class= "Increment"/>
This approach, however, seems to be best used for applications with large amounts of concurrency.
See Hiberante Reference:
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.

3 , if you use Uuid.hex the resulting random + the number of digits is the primary key,
Then the database ID field type is char with a length of 32
Written in Hbm.xml: <generator class= "Uuid.hex"/>
In addition, uuid.string is functionally similar.
Uuid.hex produces a string of 32 bits of 16 decimal digits.
And uuid.string produces a string of 16 characters of any ASCII character.
See reference:
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

Hibernate provides built-in identifier generator

Related Article

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.