<hibernate-mapping>
<class name="com.kela.hb.pojo.KelaStudent" table="KELA_STUDENT">
<!-- id 節點配置了表的主鍵-->
<id
name="stuID" //POJO類指的ID
column="StuID" //數據庫中的主鍵名稱
type="java.lang.String" //字符類型
>
<generator class="uuid.hex"/> //這里的uuid.hex代表了一種主鍵的生成方式
</id>
<property
name="stuName" //POJO中的屬性
column="StuName" //屬性對應數據庫字段的名稱
type="java.lang.String" //屬性類型
/>
<property
name="stuSex"
column="StuSex"
type="java.lang.String"
/>
<property
name="stuAge"
column="StuAge"
type="java.lang.Integer"
/>
<property
name="stuAddress"
column="StuAddress"
type="java.lang.String"
/>
</class>
</hibernate-mapping>
7. 測試代碼
HelloTest . java
package com.kela.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.kela.hb.pojo.KelaStudent;
import com.kela.hb.util.HibernateUtil;
public class HelloTest {
Session session = null;
public void testInsert() {
Transaction tran = null;
try {
//從 HibernateUtil 得到 hibernate的session, HibernateUtil .java 文件在后
session = HibernateUtil.currentSession();
//啟動事務
tran = session.beginTransaction();
KelaStudent kelaStudent = new KelaStudent();
//插入數據
kelaStudent.setStuName("王小二");
kelaStudent.setStuSex("T");
kelaStudent.setStuAge(new Integer(26));
kelaStudent.setStuAddress("甘肅蘭州");
//保存并提交事務
session.save(kelaStudent);
session.flush();
tran.commit();
} catch (HibernateException he){
System.out.println("運行中發生了錯誤");
try {
HibernateUtil.closeSession();
} catch (HibernateException he2) {
System.out.println("關閉 session 沒有成功");
}
}
}
public static void main(String[] args) {
System.out.println(" ========= 開始測試 ==========");
HelloTest helloTest = new HelloTest();
helloTest.testInsert();
System.out.println(" ========= SUCCESS ==========");
}
}
下面是 HibernateUtil . java
/**
* HibernateUtil.java
*/
package com.kela.hb.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("初始化 SessionFactory 發生錯誤.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() {
Session s = (Session) session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
8. 運行結果
運行 HelloTest.java 文件,觀察數據庫中的結果,可以看到一條數據被添加了進來。
數據庫結果:
402880fc07c68f690107c68f6dfd0001 王小二 T 26 甘肅蘭州
在控制臺看到如下日志:
Hibernate:
insert into KELA_STUDENT (StuName, StuSex, StuAge, StuAddress, StuID) values (?, ?, ?, ?, ?)
就是這么神奇。不需要指定 SQL 語句一樣能實現SQL的功能。
9. 關于測試代碼的解釋
在Hibernate中session完成持久化操作,要創建一個Session實例大致需要3個步驟:
A. 初始化Hibernate配置管理類Configuration
//讀入默認的配置文件(hibernate.cfg.xml 或 hibernate.properties)來創建一個Configuration
Configuration config = new Configuration().configure();
B. 通過Configuration類的實例來創建 Session 的工廠類 SessionFactory:
SessionFactory sessionFactory = config.buildSessionFactory();
C. 通過SessionFactory得到Session實例:
session = sessionFactory.openSession();
D. 通過Session實例完成持久化操作:
//啟動事務
tran = session.beginTransaction();
KelaStudent kelaStudent = new KelaStudent();
//插入數據
kelaStudent.setStuName("王小二");
kelaStudent.setStuSex("T");
kelaStudent.setStuAge(new Integer(26));
kelaStudent.setStuAddress("甘肅蘭州");
//保存并提交事務
session.save(kelaStudent);
session.flush();
tran.commit();
說明:由于SessionFactory采取了線程安全的設計,可以有多個線程并發調用,大多數情況下,一個應用中針對一個數據庫共享一個SessionFactory實例即可。
Session(這里的Session是Hibernate的Session)可以理解成相當于JDBC 的Connection),它的設計是非線程安全的也就是說一個Session實例只可以由一個線程使用。
有了以上兩點的考慮,在 HelloTest.java中在得到Session實例使用了:
//從 HibernateUtil 得到 hibernate的session, HibernateUtil .java 文件在后
session = HibernateUtil.currentSession();
在HibernateUtil.java 文件中創建Session,關閉Session。(這段代碼來自于Hibernate文檔)
這就相當于我們平時從數據庫連接池中得到 Connection,用完了 close.conn() 是一樣的道理。