Hibernate Generator ID

Source: Internet
Author: User
Tags uuid

The following content is organized from the network

"Assigned"
The primary key is generated by an external program and is specified before save ().
"Hilo"
The primary key generation mechanism implemented by the HI/LO algorithm requires additional database tables or fields to provide high-level value sources.
"Seqhilo"
Similar to Hilo, the primary key generation mechanism implemented by the HI/LO algorithm requires a Sequence in the database, which is suitable for databases that support Sequence, such as Oracle.
"Increment"
The primary key is incremented in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to hold the current maximum value, and then add 1 to the primary key each time a primary key is generated. The possible problem with this approach is that it cannot be used under a cluster.
"Identity"
Adopt the primary key generation mechanism provided by the database. such as DB2, SQL Server, MySQL in the primary key generation mechanism.
"Sequence"
The primary key is generated using the sequence mechanism provided by the database. such as the sequence in Oralce.
"Native"
Hibernate uses the identity, Hilo, and sequence as the primary key generation method according to the database used in its own judgment.
"Uuid.hex"
Hibernate generates 16 binary values based on the 128-bit UUID algorithm (encoded with a string of length 32) as the primary key.
"Uuid.string"
Similar to Uuid.hex, the generated primary key is not encoded (length 16) and cannot be applied to the PostgreSQL database.
"Foreign"

Use the identifier of another associated object as the primary key.
The <generator> in the <id> element is used to generate a unique identity for an instance of the persisted class, and Hibernate provides a number of built-in implementations.
Increment: Generates an identifier automatically incremented by hibernate to generate a unique identity for a long, short, or int type.
Identity: The identifier is generated by the underlying database (autogrow), and the returned identifier is of type long, short, or int.
Sequence:hibernate generates identifiers based on the underlying database sequence, and the returned identifiers are of type long, short, or int.
Hilo: Use a high/low algorithm to efficiently generate identifiers for long, short, or int types.
Uuid.hex: Generates an identifier for a 32-bit string type with a 128-bit UUID algorithm.
Native: Select identity, Sequence, or one of Hilo based on the ability of the underlying database.
Assigned: Let the application assign an identifier to the object before save ().
Foreign: An identifier for an object that is associated with another. Used in conjunction with the <one-to-one> Union.

First, the ID generation method
1, sequence sequence only for Oracle
<id name= "id" column= "id" >
<generator class= "Sequence" >
<param name= "sequence" >person_seq</param><!--specify sequence name--
</generator>
</id>
2, self-increment, for SQL Server, MYSQL
<id name= "id" column= "id" >
<generator class= "Identity"/>
</id>
3, take the maximum value plus one
<id name= "id" column= "id" type= "integer" >
<generator class= "Increment"/>
</id>
4, specify the generation method according to the underlying database
<id name= "id" column= "id" >
<generator class= "native"/>
</id>
Using the default Policy
The way to build an Oracle database is sequence, but requires a sequence of a particular name, "Hibernate_sequence".
5, high and low position algorithm
<id name= "id" column= "id" >
<generator class= "Hilo" >
<param name= "Table" >high_value</param>
<!--set the value of the high value of the table--
<param name= "column" >next_value</param>
<!--set the value of the High Value field--
<param name= "Max_lo" >50</param>
<!--Specify the maximum value of the low position, and when the maximum is taken, a high value is then calculated and then the
</generator>
</id>
These are common forms of the Hilo algorithm and are not suitable for squenece
Save multiple objects in one session
Second, hibernate in the treatment of the relationship between the class:
How the One―to―one relationship is embodied in the database and how it is embodied in the JavaBean
How to map one-to-one relationships in hibernate.
1, in the database: one table of the external health corresponding to another table of the main health, external health to add a unique constraint (external Health Association). or two tables that share a primary key, which is represented by the PK in the child table that references the parent table's PK as an external health (the Primary Health Association, the PK and FK in the child table are a field).
2, JavaBean: Add a property in the JavaBean, that is, a reference to another object, you can either one-way or two-way.
3. In Hibernate:
A, the primary health mapping: are one-to-one to use foreign generation strategy.
Take car cars and engine engines (one-to-one relationship) for example:
A, the Car.hbm.xml of the main table
<class name= "Car" table= "CAR_PK" >
<id name= "id" column= "id" type= "integer" >
<generator class= "native"/>
</id>
<property name= "name" column= "name" type= "string"/>
<one-to-one name= "engine" class= "engine" cascade= "all"/>
</class>
Note: cascade= "All" means adding, deleting, and modifying engine objects when changing the car object.
The cascade must be in the main object's mapping file
B. The wording of the schedule Engine.hbm.xml
<class name= "Engine" table= "ENGINE_PK" >
<id name= "id" column= "id" type= "integer" >
<generator class= "foreign" >
<param name= "Property" >car</param>
</generator>
</id>
<property name= "model" column= "model" type= "string"/>
<one-to-one name= "Car" class= "car" c/>
</class>
Note: C indicates that the engine uses car's main health as the external health.
Foregin indicates that the ID is generated by referencing the primary key of the table car
B, external health mapping: Use one-to-one in the main table, through Property-ref many-to-one
A, the Car.hbm.xml of the main table
<class name= "Car" table= "CAR_FK" >
<id name= "id" column= "id" type= "integer" >
<generator class= "native"/>
</id>
<property name= "name" column= "name" type= "string"/>
<one-to-one name= "engine" class= "engine"
property-ref= "Car" cascade= "Save-update"/>
</class>
Note: name= "engine" property-ref= "car" indicates that the engine table refers to the car table's main health as his external health.
Cascade= "Save-update" indicates that the engine object is incremented and modified when the car object is changed.
B. The wording of the schedule Engine.hbm.xml
<class name= "Engine" table= "ENGINE_FK" >
<id name= "id" column= "id" type= "integer" >
<generator class= "native"/>
</id>
<property name= "model" column= "model" type= "string"/>
<many-to-one name= "Car" class= "car"
Unique= "Ture" column= "Carid"/>
</class>
Note: unique= "ture" column= "Carid" represents a unique constraint for the external health carid in the engine table, forcing a one-to-many relationship into a one-to-one relationship

Hibernate Generator ID

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.