NHibernate Primary Key Generation Policy

Source: Internet
Author: User

Generator provides a unique identifier for each POCO instance. Generally, we use "native ". Class indicates an instance implemented using the Generator interface net. sf. hibernate. id. IdentifierGenerator, including:
"Assigned"

A primary key is generated by an external program and specified before save.

"Hilo"

The primary key generation mechanism implemented by the hi/lo algorithm requires additional database tables or fields to provide a high value source.

"Seqhilo"

Similar to hilo, the primary key generation mechanism implemented by the hi/lo algorithm requires Sequence in the database, which is suitable for databases that support Sequence, such as Oracle.

"Increment"

The primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then add 1 as the primary key each time the primary key needs to be generated. This method may cause problems: it cannot be used in a cluster.

"Identity"

Use the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL.

"Sequence"

Use the sequence mechanism provided by the database to generate the primary key. For example, Sequence in Oralce.

"Native"

Hibernate uses identity, hilo, and sequence as the primary key generation method based on the database used.

"Uuid. hex"

Hibernate uses the 128-bit UUID algorithm to generate a hexadecimal value (represented by a 32-Bit String After encoding) as the primary key.

"Uuid. string"

Similar to uuid. hex, the generated primary key is not encoded (Length: 16) and cannot be used in the PostgreSQL database.

"Foreign"

Use the identifier of another associated object as the primary key.

Uuid. hex is used as an example to describe the specific usage:

1. Create a table in the database. The primary key name is ID and the type is varchar2 (string type)

2. Configure the following in **. hbm. xml (hibernate ing file ):
<Class name = "com. jat. bisarea. ho. Test" table = "BA_VVTEST">
<Id name = "id" type = "string" column = "ID">
// Use the uuid. hex policy that comes with hibernate to generate a primary key.
<Generator class = "uuid. hex"/> // the data type and policy details are described below
</Id>
<Property name = "uname" type = "java. lang. String" column = "UNAME"/>
</Class>

3. When adding a record to a table in the java file, you only need to add other fields except the ID and save them. The related java code is as follows:

Session s = HibernateUtil. currentSession ();
Transaction tx = s. beginTransaction ();

Test test = new Test ();
String uname = httpServletRequest. getParameter ("uname ");
Test. setUname (uname );
// You only need to set the uname, And the id is generated by hibernate.
S. save (test );
Tx. commit ();
4. The methods using other policies are basically the same, such as hilo and seqhilo.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/lryain/archive/2008/08/04/2764176.aspx

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.