Hibernate primary Key generation policy

Source: Internet
Author: User
Tags throw exception uuid

First step: Introduce the jar package omission

Part II: Creating an entity class

Package Cn.hibernate.bean;

public class Student {

private Integer sId;
private String sName;
Public Integer GetSID () {
return sId;
}
Public void Setsid (Integer sId) {
this.sid = sId;
}
Public String Getsname () {
return sName;
}
Public void Setsname (String sName) {
this.sname = sName;
}
Public Student (Integer sId, String sName) {
super ();
this.sid = sId;
this.sname = sName;
}
Public Student () {
super ();
}
@Override
Public String toString () {
return "Student [sid=" + SId + ", sname=" + SName + "]";
}

}

Step three: Create a mapping file Student.hbm.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD" >

<class name= "Student" >
<id name= "SId" >
<!--
primary key generation policy
* Shaping: Int/short/long
1.increment:hiernate The primary key in the Automatic maintenance table, query Max (ID) First, then the maximum value +1, execute the INSERT statement
There is a concurrency problem and it is not recommended
the 2.identity,hibernate is automatically normal with the database underlying. Example: MySQL # # auto_increment modifier primary KEY
"auto--increment=5" exists in the table structure of the database for logging
The 3.sequence,hibernate uses the database underlying sequence. Example: Oracle
Oracle: Automatic enhancement not supported, but sequence supported
4.hilo,hibernate will use the high-low algorithm (High/low) for data that does not know about this autogrow and sequence (understand it)
thought: Using another table to record the accumulated data
<generator class= "Hilo" >
<param name= "table" >hi_value</param> hi_value another table indicates
<param name= "column" >next_value</param> next_value another table field name
<param name= "Max_lo" >100</param> the maximum number of concurrent operations supported by a single operation.
</generator>
Batch First value: Max_lo * next_value + next_value
6 + 6
5.native: Select one from identity, sequence, Hilo, according to database decision
# # #以上的类型都是整形
6.UUID, type must be string
above policy: Hibernate auto Generation: Proxy primary key
7.assigned: Natural primary key, the program set itself.
-
<generator class= "Assigned" >
</generator>
</id>
<property name= "SName" ></property>
</class>

Fourth step: Create a Hibernate.cfg.xml file

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-CONFIGURATION-3.0.DTD" >

<session-factory>
<!--#1 basic four items
Property.Name value if you start with ' hibernate ', you can omit
same as "Hibernate.connection.driver_class" and "Connection.driver_class"
-
<property name= "Hibernate.connection.driver_class" >
Com.mysql.jdbc.Driver
</property>
<property name= "Hibernate.connection.url" >
Jdbc:mysql://localhost:3306/minemysql
</property>
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >root</property>

<!--#2方言-
<property name= "Hibernate.dialect" >
Org.hibernate.dialect.MySQLDialect
</property>


<!--#3 SQL --
<!--whether to display SQL statements --
<property name= "Hibernate.show_sql" >true</property>
<!--whether formatted SQL statements do not format the display of a row formatted to display multiple lines--
<property name= "Hibernate.format_sql" >true</property>
<!--Whether a comment is displayed, providing the current SQL statement action Object--
<property name= "hibernate.use_sql_comments" >true</property>


<!--#4如何创建表 (not important)
Create : Each time the Cfg.xml file is loaded, the table will be created, and when the program is closed, the table is not deleted [initialized, used during testing]
If the table exists, it is first deleted and then created
Create-drop: Each time the Cfg.xml file is loaded, the table is created and the table is deleted when the program closes
Factory.close () must be executed to delete
Update: If the table does not exist then create, if the table exists, first check whether the *.hbm.xml file matches the table,
if the mismatch will update the table structure (add only, do not delete)
Validate: Load cfg.xml validation, map file and data table match, if match normal operation, if not match throw exception
# # # shows the development of the first table, and then the mapping file
* Table created by DBA
-
<property name= "Hibernate.hbm2ddl.auto" >update</property>


<!--#5取消bean效验-
<property name= "Javax.persistence.validation.mode" >none</property>
</session-factory>

Fifth step: Create a Test class

Package cn.hibernate.test;

Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Import Org.junit.Test;

Import cn.hibernate.bean.Student;

Public class Teststu {

private Sessionfactory factory = new Configuration (). Configure (). addclass (Student.class). Buildsessionfactory ();

@Test
Public void Demo01 () {
/*
* Test increment in the primary key generation strategy
* */

Session session = Factory.opensession ();
session.begintransaction ();

Student stu = new Student ();
stu.setsname ("haha");
Session.save (stu);
session.gettransaction (). commit ();
session.close ();

//Execute the statement
/*hibernate:
Select
Max (sId)
from
Student
Hibernate:
Insert Cn.hibernate.bean.Student
Insert
into
Student
(SName, sId)
Values
(?, ?) */



}


@Test
Public void Demo02 () {
/*
* Test the identity in the primary key generation policy
* */

Session session = Factory.opensession ();
session.begintransaction ();

Student stu = new Student ();
stu.setsname ("haha 1");
Session.save (stu);
session.gettransaction (). commit ();
session.close ();

//Execute the statement
/*hibernate:
Insert Cn.hibernate.bean.Student
Insert
into
Student
(SName)
Values
(?)
*/



}


@Test
Public void demo03 () {
/*
* Test the Hilo in the primary key generation strategy
* */

Session session = Factory.opensession ();
session.begintransaction ();

Student stu = new Student ();
stu.setsname ("haha");
Session.save (stu);

Student stu1 = new Student ();
stu.setsname ("haha");
Session.save (STU1);

Student stu2 = new Student ();
stu.setsname ("haha");
Session.save (STU2);

session.gettransaction (). commit ();
session.close ();

}

@Test
Public void demo04 () {
/*
* Test the UUID in the primary key generation strategy
* */

Session session = Factory.opensession ();
session.begintransaction ();

Student stu = new Student ();
stu.setsname ("333");
Session.save (stu);

Student stu1 = new Student ();
stu.setsname ("222");
Session.save (STU1);

Student stu2 = new Student ();
Stu.setsname ("111");
Session.save (STU2);

session.gettransaction (). commit ();
session.close ();

}

@Test
public void demo05 () {
/*
* Test assigned in the primary key generation strategy
* */

Session session = Factory.opensession ();
Session.begintransaction ();

Student stu = new Student ();
Stu.setsid ("104322-19910423-ww");
Stu.setsname ("333");
Session.save (Stu);

Session.gettransaction (). commit ();
Session.close ();

}

}

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.