Hibernate primary key Generation policy

Source: Internet
Author: User

1.increment

Applies to the proxy primary key. The table identifier is automatically generated incrementally by hibernate, with each increment of 1.
Mysql
Hibernate:select Max (ID) from users
Hibernate:insert into Hjd.users (name, pass, sex, id) VALUES (?,?,?,?)
Applies to all databases.

2.identity

Applies to the proxy primary key. The table identifier is generated by the underlying database. The condition is that the database supports auto-grow data types.
Hibernate:insert into Hjd.users (name, pass, sex) VALUES (?,?,?)
For MySQL DB2 SQL Server Sybase, etc.

The sequence applies to the proxy primary key. Hibernate generates identifiers based on the underlying database sequence. The condition is that the database supports sequences.
DB2 Oracle, etc.

3.hilo

Applies to the proxy primary key. Hibernate generates identifiers based on the hign/low algorithm. Hibernate takes the field of a particular table as the "hign" value. By default, the Next_hi field of the Hibernate_unique_key table is used.

<generator class= "Hilo";
                 <param name= "table" >HILO_IDS</PARAM>
                 <param name= "column" >VALUE</PARAM>
                 <param name= "Max_lo" >100</PARAM>
            </generator>

hibernate:create table hilo_ids (Value integer)
Hibernate: INSERT into Hilo_ids values (0)
April 24, 2015 2:37:56 pm Org.hibernate.tool.hbm2ddl.SchemaExport Execute
info:hhh00 0230:schema Export Complete
Hibernate:select value from Hilo_ids for update
Hibernate:update hilo_ids Set value = ? where value =?
Hibernate:insert into Hjd.users (name, pass, sex, id) VALUES (?,?,?,?)


4.NATIVE&NBSP;&NBSP;  

Applies to the proxy primary key. Select identity, Sequence, Hilo based on the ability of the underlying database to automatically generate the representation


5.uuid.hex

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


6.assigned

Applies to natural primary keys. The Java program is responsible for generating identifiers. The SetID () method cannot be declared private. Try to avoid using natural primary keys.

public class Dept implements serializable{

Private static final long serialversionuid = 1L;
private String name;
Private Date CDate;
Public Dept () {
Super ();
TODO auto-generated Constructor stub
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public Date getcdate () {
return cdate;
}
public void Setcdate (Date cdate) {
This.cdate = CDate;
}
}
<class name= "Dept" table= "depts" catalog= "Hjd" >
<!--ID PRIMARY KEY--
<id name= "name" column= "name" type= "string" >
<generator class= "Assigned"/>
</id>
<!--class Properties-
<property name= "CDate" column= "CDate" type= "date"/>
</class>

7. Composite PRIMARY KEY:

The first form of a notation

public class Customer implements Serializable {

Private static final long serialversionuid = 1L;
Private String FirstName;
Private String LastName;
Private String sex;
Public Customer () {
Super ();
TODO auto-generated Constructor stub
}
Public String Getfirstname () {
return firstName;
}
public void Setfirstname (String firstName) {
This.firstname = FirstName;
}
Public String Getlastname () {
return lastName;
}
public void Setlastname (String lastName) {
This.lastname = LastName;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
}

<class name= "Customer" table= "Customers" catalog= "HJD" >
<composite-id>
<key-property name= "FirstName" length= "" "type=" string "/>
<key-property name= "LastName" length= "" "type=" string "/>
</composite-id>
<property name= "Sex" length= "4" ></property>
</class>

@Test
public void Save () {
1. Get the Configuration object
Configuration configuration = new configuration (). Configure ();
2. Get the Sessionfactory object
Sessionfactory sessionfactory = Configuration.buildsessionfactory ();
3. Get session, open transaction
Session session = Sessionfactory.opensession ();
Transaction ts = session.begintransaction ();
Customer Customer=new customer ();
Customer.setfirstname ("zz");
Customer.setlastname ("SF");
Customer.setsex ("female");

try {
Session.save (customer);
} catch (Exception e) {
Ts.rollback ();
E.printstacktrace ();
}
Ts.commit ();
Session.close ();
}

The second way:

public class Customer implements Serializable {

Private static final long serialversionuid = 1L;
Private CustomerId CustomerId;
Private String sex;
Public Customer () {
Super ();
TODO auto-generated Constructor stub
}
Public CustomerId Getcustomerid () {
return customerId;
}
public void Setcustomerid (CustomerId CustomerId) {
This.customerid = customerId;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
}

public class CustomerId implements serializable{
Private static final long serialversionuid = 1L;
Private String FirstName;
Private String LastName;
Public CustomerId () {
Super ();
TODO auto-generated Constructor stub
}
Public String Getfirstname () {
return firstName;
}
public void Setfirstname (String firstName) {
This.firstname = FirstName;
}
Public String Getlastname () {
return lastName;
}
public void Setlastname (String lastName) {
This.lastname = LastName;
}
}

<class name= "Customer" table= "Customers" catalog= "HJD" >
<composite-id name= "customerId" class= "CustomerId" >
<key-property name= "FirstName" length= "" "type=" string "/>
<key-property name= "LastName" length= "" "type=" string "/>
</composite-id>
<property name= "Sex" length= "4" ></property>
</class>

Note: All two classes need to implement serializable interfaces, or they will get an error

Hibernate primary key 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.