<hibernate-mapping>
<class name="com.kela.hb.pojo.KelaStudent" table="KELA_STUDENT">
<!-- id 節(jié)點(diǎn)配置了表的主鍵-->
<id
name="stuID" //POJO類指的ID
column="StuID" //數(shù)據(jù)庫(kù)中的主鍵名稱
type="java.lang.String" //字符類型
>
<generator class="uuid.hex"/> //這里的uuid.hex代表了一種主鍵的生成方式
</id>
<property
name="stuName" //POJO中的屬性
column="StuName" //屬性對(duì)應(yīng)數(shù)據(jù)庫(kù)字段的名稱
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. 測(cè)試代碼
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();
//啟動(dòng)事務(wù)
tran = session.beginTransaction();
KelaStudent kelaStudent = new KelaStudent();
//插入數(shù)據(jù)
kelaStudent.setStuName("王小二");
kelaStudent.setStuSex("T");
kelaStudent.setStuAge(new Integer(26));
kelaStudent.setStuAddress("甘肅蘭州");
//保存并提交事務(wù)
session.save(kelaStudent);
session.flush();
tran.commit();
} catch (HibernateException he){
System.out.println("運(yùn)行中發(fā)生了錯(cuò)誤");
try {
HibernateUtil.closeSession();
} catch (HibernateException he2) {
System.out.println("關(guān)閉 session 沒有成功");
}
}
}
public static void main(String[] args) {
System.out.println(" ========= 開始測(cè)試 ==========");
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 發(fā)生錯(cuò)誤.", 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. 運(yùn)行結(jié)果
運(yùn)行 HelloTest.java 文件,觀察數(shù)據(jù)庫(kù)中的結(jié)果,可以看到一條數(shù)據(jù)被添加了進(jìn)來(lái)。
數(shù)據(jù)庫(kù)結(jié)果:
402880fc07c68f690107c68f6dfd0001 王小二 T 26 甘肅蘭州
在控制臺(tái)看到如下日志:
Hibernate:
insert into KELA_STUDENT (StuName, StuSex, StuAge, StuAddress, StuID) values (?, ?, ?, ?, ?)
就是這么神奇。不需要指定 SQL 語(yǔ)句一樣能實(shí)現(xiàn)SQL的功能。
9. 關(guān)于測(cè)試代碼的解釋
在Hibernate中session完成持久化操作,要?jiǎng)?chuàng)建一個(gè)Session實(shí)例大致需要3個(gè)步驟:
A. 初始化Hibernate配置管理類Configuration
//讀入默認(rèn)的配置文件(hibernate.cfg.xml 或 hibernate.properties)來(lái)創(chuàng)建一個(gè)Configuration
Configuration config = new Configuration().configure();
B. 通過(guò)Configuration類的實(shí)例來(lái)創(chuàng)建 Session 的工廠類 SessionFactory:
SessionFactory sessionFactory = config.buildSessionFactory();
C. 通過(guò)SessionFactory得到Session實(shí)例:
session = sessionFactory.openSession();
D. 通過(guò)Session實(shí)例完成持久化操作:
//啟動(dòng)事務(wù)
tran = session.beginTransaction();
KelaStudent kelaStudent = new KelaStudent();
//插入數(shù)據(jù)
kelaStudent.setStuName("王小二");
kelaStudent.setStuSex("T");
kelaStudent.setStuAge(new Integer(26));
kelaStudent.setStuAddress("甘肅蘭州");
//保存并提交事務(wù)
session.save(kelaStudent);
session.flush();
tran.commit();
說(shuō)明:由于SessionFactory采取了線程安全的設(shè)計(jì),可以有多個(gè)線程并發(fā)調(diào)用,大多數(shù)情況下,一個(gè)應(yīng)用中針對(duì)一個(gè)數(shù)據(jù)庫(kù)共享一個(gè)SessionFactory實(shí)例即可。
Session(這里的Session是Hibernate的Session)可以理解成相當(dāng)于JDBC 的Connection),它的設(shè)計(jì)是非線程安全的也就是說(shuō)一個(gè)Session實(shí)例只可以由一個(gè)線程使用。
有了以上兩點(diǎn)的考慮,在 HelloTest.java中在得到Session實(shí)例使用了:
//從 HibernateUtil 得到 hibernate的session, HibernateUtil .java 文件在后
session = HibernateUtil.currentSession();
在HibernateUtil.java 文件中創(chuàng)建Session,關(guān)閉Session。(這段代碼來(lái)自于Hibernate文檔)
這就相當(dāng)于我們平時(shí)從數(shù)據(jù)庫(kù)連接池中得到 Connection,用完了 close.conn() 是一樣的道理。