Hibernate learning Note (4) ID generation policy

Source: Internet
Author: User

ID of an Object relational database mapping

The mapped class must define the corresponding database table primary key field. Most classes have a JavaBeans-style attribute that contains a unique identity for each instance. The <id> element defines the mapping of this property to the primary key field of the database table.

<id        name= "PropertyName"        type= "TypeName"        column= "column_name"        unsaved-value= "null|any|none| Undefined|id_value "        access=" field|property| ClassName "        node=" element-name| @attribute-name|element/@attribute |. " >        <generator class= "Generatorclass"/></id>


The main explanation here is the use of the ID generation strategy, generator

two Generator (ID generation policy)

The optional <generator> child element is the name of a Java class that is used to generate a unique identity for an instance of the persisted class. If the generator instance requires some configuration values or initialization parameters, it is passed with the <param> element.

<id name= "id" type= "long" column= "cat_id" >        <generator class= "Org.hibernate.id.TableHiLoGenerator" >                <param name= "table" >uid_table</param>                <param name= "column" >next_hi_value_column</ Param>        </generator></id>
All generators implement the Org.hibernate.id.IdentifierGenerator interface. 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 some quick names for the built-in generators:

(1) UUID

A 128-bit UUID algorithm is used to generate the identifier for the string type, which is unique across a network (using an IP address). The UUID is encoded as a string of 32-bit 16 decimal digits.

Using XML Configuration Method

Auto-Generate ID:



An automatically generated ID of type string using the UUID method




(2) native

Different IDs are generated depending on the database, for example: Using identity in SQL Server; The use of auto_increment in MySQL; Sequence is used in Oracle, and note that Hibernate automatically helps you create a sequence of names called Hibernate_sequence without having to create them yourself. This is also the most common and convenient.

Using XML Configuration Method

Use annotation method:

@Entitypublic class Studentinfo {private int id; @Id @generatedvalue (strategy = generationtype.auto) public  int GetId () {return ID;} public void setId (int id) {this.id = id;}}


Auto-Generate ID:



(3) 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.

When using SQL Server, this strategy is equivalent to the IDENTITY keyword of SQL Server, which is equivalent to MySQL's auto_increment keyword when using mysql and cannot be used in Oracle.

The XML configuration method is used:

Use annotation method:

@Entitypublic class Studentinfo {private int id; @Id @generatedvalue (strategy = generationtype.identity) Public  int GetId () {return ID;} public void setId (int id) {this.id = id;}}

(4)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.

The XML configuration method is used:


Use annotation method:

@Entity @sequencegenerator (name = "Studentinfoseq", Sequencename = "studentinfoseq_db") public class Studentinfo { private int id; @Id @generatedvalue (strategy = generationtype.sequence, generator = "studentinfoseq") public  int GetId () {return ID;} public void setId (int id) {this.id = id;}}

(5)Hilo

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 the source of the high value. The high/low algorithm generates identifiers that are unique only in a particular database.

(6) Seqhilo uses a high/low algorithm to efficiently generate identifiers of long, short, or int types, given the name of a database sequence (sequence).

(7) increment is used to generate a unique identifier 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.


Hibernate learning Note (4) ID generation policy

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.