[Hibernate Development Road] (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 includes unique identities 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>


Here is the main explanation of the ID generation strategy, the use of generator

two Generator (ID generation policy)

The optional <generator> child element is the name of a Java class. Used to generate a unique identity for an instance of the persisted class. Assume that the generator instance requires some configuration values or initialization parameters to be 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 easy 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 the XML configuration method

Proactively generate ID:



Use UUID to generate self-generated ID string type




(2) native

Different IDs are generated according to different databases, for example: Using identity in SQL Server; The use of auto_increment in MySQL; Using sequence in Oracle, note that hibernate will help you create a sequence of names called Hibernate_sequence without having to create them yourself. This is also the most frequently used and easy to use.

Using the XML configuration method

To use the annotated 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;}}


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

Such a strategy when using SQL Server, the equivalent of SQL Server Identitykeyword, in the use of MySQL equivalent to MySQL Auto_incrementkeyword, can not be used in Oracle.

The XML configuration method is used:

To use the annotated 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

Use sequences (sequence) in Db2,postgresql, Oracle, SAP DB, Mckoi. Instead, use the generator (generator) in InterBase. The returned identifiers are of type long, short, or int.

The XML configuration method is used:


To use the annotated 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;}}


Write in front of the class

@Enterty

@SequenceGenerator (name= "", Sequencename= "")//sequence generator. The first number of parameters is the name of the generator. The second parameter refers to the name of the sequence that was generated into the database.

Write before the primary key method:

@Id

@GeneratedValue (strategy=generationtype.sequence, generator= "name of the generator defined above")//Specify the generator's policy

(5)Hilo

Use a high/low algorithm to efficiently generate identifiers for long, short, or int types.

Given a table and a field (default is Hibernate_unique_key and Next_hi) as the source of the high value.

The identifier generated by the high/low algorithm is 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 is only available when no other process is inserting data into the same table.

Do not use under the cluster.

(8) Table

Using a single data table to manage the primary key generation of all data tables is the most troublesome, but it is very convenient to use well, especially for large projects, data sheets, and many more. Here are just a few examples of annotations

@Entity @tablegenerator (name = "STUDENTINFO_TG", table = "Tablegenerator_table", Pkcolumnname = "key", Valuecolumnname = " Value ", Pkcolumnvalue =" Studentinfo ", allocationsize = 1) public class Studentinfo {private int id; @GeneratedValue ( Strategy = generationtype.table, generator = "STUDENTINFO_TG") public int getId () {return ID;} public void setId (int id) {this.id = id;}}

The @TableGenerator indicates that the table is to be generated using the ID of table, and the parameters in parentheses are as follows:
Name indicates the name of the generator and is indicated in the ID of the subsequent entity.
Table represents the names of the tables that generate the policy for the management ID generated in the database.
Pkcolumnname table field name indicates the name of the primary key
Pkcolumnvalue table field name represents the value of the primary key


Valuecolumnname represents the name of the primary key to be recorded for this table, such as this is Studentinfo, then the studentinfo is used in the STUDENTINFO_TG table to record the current ID value
Allocationsize represents the step value that grows after each acquisition of the value of an ID, which is incremented by 1 each time.

Table structure:

Key Value
Studentinfo 2

Each time you need a primary key value, query the table named "Tablegenerator_table", look for a record with the field "key" value "Studentinfo", get the value of this record is 2, according to this value, And the value of allocationsize to generate a primary key value.


Can actually be understood as:

Select value from tablegenerator_table where key = ' studentinfo '






[Hibernate Development Road] (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.