Hibernate 主鍵建置原則

來源:互聯網
上載者:User

標籤:資料庫   uil   實體   config   建議   seq   XML   tab   ora   

第一步:引入jar包 省略

第二部:建立實體類

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 + "]";
}

}

第三步:建立對應檔 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">
<hibernate-mapping package="cn.hibernate.bean">
<class name="Student">
<id name="sId">
<!--
主鍵建置原則
* 整形 :int/short/long
1.increment:hiernate自動維護表中的主鍵,先進行查詢max(id),然後最大值+1,執行insert語句
存在並發問題,不建議使用
2.identity,hibernate採用資料庫底層的自動正常。例如:mysql ## auto_increment修飾主鍵
資料庫的表結構中存在“AUTO——increment=5”進行記錄
3.sequence,hibernate採用資料庫底層的序列。例如:oracle
oracle:不支援自動增強,但支援序列
4.hilo,hibernate將採用高低位演算法(high/low)適用於不知此自動成長和序列的資料(瞭解即可)
思想:採用另一張表記錄累加資料
<generator class="hilo">
<param name="table">hi_value</param> hi_value 另一張表的表明
<param name="column">next_value</param> next_value 另一張表的欄位名
<param name="max_lo">100</param> 一次操作支援的最大操作並發數。
</generator>
批量第一個值:max_lo * next_value + next_value
100 6 + 6
5.native:從identity、sequence、hilo 三選一,根據資料庫決定
###以上的類型都是整形
6.UUID,類型必須字串
以上策略:hibernate自動產生:代理主鍵
7.assigned:自然主鍵,程式自己設定。
-->
<generator class="assigned">
</generator>
</id>
<property name="sName"></property>
</class>
</hibernate-mapping>

第四步:建立hibernate.cfg.xml檔案

<?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">
<hibernate-configuration>
<session-factory>
<!-- #1 基本的四項
property.name 取值如果以‘hibernate’開頭,可以省略
以“hibernate.connection.driver_class”與“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 -->
<!-- 是否顯示sql語句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否格式化sql語句 不格式化顯示一行 格式化顯示多行 -->
<property name="hibernate.format_sql">true</property>
<!-- 是否顯示注釋,提供當前sql語句操作對象 -->
<property name="hibernate.use_sql_comments">true</property>


<!-- #4如何建立表(不重要)
create:每一次載入cfg.xml檔案都將建立表,程式關閉時,表不進行刪除 [初始化,測試時使用]
如果表存在則先刪除後建立
create-drop:每一次載入cfg.xml檔案都將建立表,程式關閉時,表進行刪除
必須執行factory.close()才能刪除
update:如果表不存在則建立,如果表存在,先回檢查*.hbm.xml檔案是否和表匹配,
如果不匹配將更新表結構(只添加,不刪除)
validate:載入cfg.xml進效驗,對應檔和資料表是否匹配,如果匹配正常操作,如果不匹配則拋出異常
### 顯示的開發中先有的表,再有的對應檔
* 表 由DBA建立
-->
<property name="hibernate.hbm2ddl.auto">update</property>


<!-- #5取消bean效驗 -->
<property name="javax.persistence.validation.mode">none</property>
</session-factory>
</hibernate-configuration>

第五步:建立測試類別

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(){
/*
* 測試主鍵建置原則中的 increment
* */

Session session = factory.openSession();
session.beginTransaction();

Student stu = new Student();
stu.setsName("哈哈");
session.save(stu);
session.getTransaction().commit();
session.close();

//執行的語句
/*Hibernate:
select
max(sId)
from
Student
Hibernate:
insert cn.hibernate.bean.Student
insert
into
Student
(sName, sId)
values
(?, ?)*/



}


@Test
public void demo02(){
/*
* 測試主鍵建置原則中的 identity
* */

Session session = factory.openSession();
session.beginTransaction();

Student stu = new Student();
stu.setsName("哈哈1");
session.save(stu);
session.getTransaction().commit();
session.close();

//執行的語句
/*Hibernate:
insert cn.hibernate.bean.Student
insert
into
Student
(sName)
values
(?)
*/



}


@Test
public void demo03(){
/*
* 測試主鍵建置原則中的 hilo
* */

Session session = factory.openSession();
session.beginTransaction();

Student stu = new Student();
stu.setsName("哈哈12");
session.save(stu);

Student stu1 = new Student();
stu.setsName("哈哈13");
session.save(stu1);

Student stu2 = new Student();
stu.setsName("哈哈14");
session.save(stu2);

session.getTransaction().commit();
session.close();

}

@Test
public void demo04(){
/*
* 測試主鍵建置原則中的 UUID
* */

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(){
/*
* 測試主鍵建置原則中的 assigned
* */

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 主鍵建置原則

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.