創建一個實體類: package domain; public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_indus ...
創建一個實體類:
package domain; public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]"; } }View Code
ORM元數據配置文件:
<?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> <class name="domain.Customer" table="cst_customer" > <id name="cust_id" > <!-- generator:主鍵生成策略 --> <generator class="native"></generator> </id> <property name="cust_name" column="cust_name" ></property> <property name="cust_source" column="cust_source" ></property> <property name="cust_industry" column="cust_industry" ></property> <property name="cust_level" column="cust_level" ></property> <property name="cust_linkman" column="cust_linkman" ></property> <property name="cust_phone" column="cust_phone" ></property> <property name="cust_mobile" column="cust_mobile" ></property> </class> </hibernate-mapping>
這裡的主鍵生成策略是native
詳細:
主鍵生成策略.就是每條記錄錄入時,主鍵的生成規則.(7個)
identity : 主鍵自增。由資料庫來維護主鍵值。錄入時不需要指定主鍵。
sequence: Oracle中的主鍵生成策略,這裡使用的是MySQL,暫時不介紹
increment(不使用,存在效率低和線程安全問題): 主鍵自增.由hibernate來維護.每次插入前會先查詢表中id最大值.+1作為新主鍵值.
hilo(不使用,瞭解即可):高低位演算法,主鍵自增。由hibernate來維護,開發時不使用。
native:hilo+sequence+identity 自動三選一策略(檢測當前資料庫,使用相應的策略)
uuid: 產生理論上永遠不會重覆的隨機字元串作為主鍵. 主鍵類型必須為string 類型.
assigned:自然主鍵生成策略。hibernate不會管理主鍵值,由開發人員自己錄入。
hibernate對象分為三種狀態:
瞬時狀態:沒有id,沒有session關聯
持久化狀態:有id,與session關聯
游離狀態(托管狀態):有id,沒有與session關聯
package state; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import domain.Customer; import utils.HibernateUtils; //測試對象的三種狀態 public class Demo { @Test //查看三種狀態 public void fun1(){ //1 獲得session Session session = HibernateUtils.openSession(); //2 控制事務 Transaction tx = session.beginTransaction(); //3執行操作 Customer c = new Customer(); // 沒有id, 沒有與session關聯 => 瞬時狀態 c.setCust_name("聯想"); // 瞬時狀態 session.save(c); // 持久化狀態, 有id,有關聯 //4提交事務.關閉資源 tx.commit(); session.close();// 游離|托管 狀態, 有id , 沒有關聯 } @Test //三種狀態特點 //save方法: 其實不能理解成保存.理解成將瞬時狀態轉換成持久狀態的方法 //主鍵自增 : 執行save方法時,為了將對象轉換為持久化狀態.必鬚生成id值.所以需要執行insert語句生成. //如果主鍵生成策略是increment: 執行save方法,為了生成id.會執行查詢id最大值的sql語句(select max(id) from 表). public void fun2(){ //1 獲得session Session session = HibernateUtils.openSession(); //2 控制事務 Transaction tx = session.beginTransaction(); //3執行操作 Customer c = new Customer(); // 沒有id, 沒有與session關聯 => 瞬時狀態 c.setCust_name("聯想"); // 瞬時狀態 session.save(c); // 持久化狀態, 有id,有關聯 //4提交事務.關閉資源 tx.commit(); session.close();// 游離|托管 狀態, 有id , 沒有關聯 } @Test //三種狀態特點 // 持久化狀態特點: 持久化狀態對象的任何變化都會自動同步到資料庫中. public void fun3(){ //1 獲得session Session session = HibernateUtils.openSession(); //2 控制事務 Transaction tx = session.beginTransaction(); //3執行操作 Customer c = session.get(Customer.class, 1l);//持久化狀態對象 c.setCust_name("微軟公司"); //4提交事務.關閉資源 tx.commit(); session.close();// 游離|托管 狀態, 有id , 沒有關聯 } }
接下來從狀態角度分析hibernate的增刪改查: