【SSH之旅】一步步學習Hibernate框架(一):關于持久化
來源:程序員人生 發布時間:2014-11-24 08:02:04 閱讀次數:2269次
在不援用任何框架下,我們會通過平凡的代碼不停的對http://www.vxbq.cn/db/進行操作,產生了很多冗余的但是又有規律的底層代碼,這樣頻繁的操作http://www.vxbq.cn/db/和大量的底層代碼的重復書寫極大的浪費了程序人員的書寫,就在這樣1種情況下,Hibernate框架應運而生了。
其實Hibernate封裝的就是Model模型中的model,封裝的這部份又叫持久層,在這層中對對象進行添加、刪除、更新、查詢等操作就叫做持久化。
持久化的對象有3種狀態:Transient Objects、Persist Objects、Detached Objects。
Transient Objects:
使用new操作初始化的對象不會被立刻持久化,其狀態時瞬時的,不被Session管理,此時,也沒有任何跟http://www.vxbq.cn/db/相干聯的行動,即http://www.vxbq.cn/db/中沒有這個記錄,只要不被其他對象援用,他們的狀態將會丟失,并由垃圾回收機制回收。
在以下情況下對象處于此狀態:
(1) 當通過new語句剛創建了1個對象,不和http://www.vxbq.cn/db/中的任何記錄對應。
(2) Session的delete()方法能使1個持久化對象或游離對象轉變成臨時對象。對游離對象,delete()方法從http://www.vxbq.cn/db/中刪除與它對應的記錄;對持久化對象,delete()方法從http://www.vxbq.cn/db/中刪除與它對應的記錄,并且把它從Session的緩存中刪除。
Persist Objects:
持久實例是任何具有http://www.vxbq.cn/db/標識的實例,它有持久化管理器Session統1管理,持久化實例在事務中進行操作的,其狀態在事務結束時同http://www.vxbq.cn/db/進行同步。當事務提交時,通過履行SQL的insert、update、delete語句把內存中的狀態同步到http://www.vxbq.cn/db/中。
Session的許多方法都能夠觸發Java對象進入持久化狀態,比如說Session的save()把臨時對象轉變成持久化對象。load()或get()返回的對象處于持久化狀態。find()返回的List集合中寄存的都是持久化對象。update()、saveOrUpdate()和lock()使游離對象轉變成持久化對象。
Detached Objects:
Session被關閉后,持久化對象就變成離線對象。離線表示這個對象不再與http://www.vxbq.cn/db/保持同步,不再受管理。
當調用Session的close()時,Session的緩存被清,緩存中的所有持久化對象都變成游離對象。evict()能夠從緩存中刪除1個持久化對象,使它變成游離狀態。
實例分析:
項目結構:

hibernate.cfg.xml文件:
配置http://www.vxbq.cn/db/信息和生成的表的
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration⑶.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">zhudan</property>
<property name="hibernate.connection.password">1221</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="zhudan/hibernate/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
User.hbm.xml文件:
需要映照的表的配置。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping⑶.0.dtd">
<hibernate-mapping >
<class name="zhudan.hibernate.User" table="T_User">
<id name="id" column="UserID">
<generator class="uuid"/>
</id>
<property name="name" column="UserName"></property>
<property name="password" column="PassWord"></property>
<property name="createTime" column="CreateTime"></property>
<property name="expireTime" column="ExpireTime"></property>
</class>
</hibernate-mapping>
HibernateUtils類:
讀取前面配置的hibernate.cfg.xml,建立SessionFactory、Session,關閉Session等。
package zhudan.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateUtils {
private static SessionFactory factory;
static{
try{
//讀取hibernate.cfg.xml
Configuration cfg=new Configuration().configure();
//建立SessionFactory
factory=cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
//獲得Session
public static Session getSession(){
return factory.openSession();
}
//關閉Session
public static void closeSession(Session session){
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
//SessionFactory
public static SessionFactory getSessionFactory(){
return factory;
}
}
ExportDB類:映照表,將hbm.xml生成對應的ddl。
package zhudan.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args){
//讀取hibernate.cfg.xml文件
Configuration cfg=new Configuration().configure();
SchemaExport export=new SchemaExport(cfg);
export.create(true, true);
}
}
SessionTest類:
package zhudan.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import junit.framework.TestCase;
public class SessionTest extends TestCase {
public void testSave1(){
Session session=null;
//開啟事務
Transaction tx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
//Transient狀態
User user=new User();
user.setName("zhudan");
user.setPassword("zhudan");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//被session管理,persistent狀態,當對象屬性改變的時候
//清算緩存(臟數據)的時候,會和http://www.vxbq.cn/db/同步
session.save(user);
//更新上1條數據
user.setName("fanglin1");
session.update(user);
//提交事務
tx.commit();
}catch(Exception e){
e.printStackTrace();
if(tx!=null){
tx.rollback();
}
}finally{
HibernateUtils.closeSession(session);
}
//detached狀態
}
}
在這個例子中,當New1個User對象時,狀態是
Transient Objects,此時http://www.vxbq.cn/db/中沒有這條數據,不被Session管理,當在履行保存時,狀態改變成Persist Objects,此時,數據中存在當前數據,被Session管理,1旦事務被提交,履行完此語句后,狀態更改成Detached
Objects,此時http://www.vxbq.cn/db/中存在此數據,被Session管理。
總結:
持久化封裝了數據訪問細節,為大部份業務邏輯提供了面向對象的接口,減少了http://www.vxbq.cn/db/訪問次數,增加了利用程序的履行速度,使其不依賴于底層http://www.vxbq.cn/db/和上層業務邏輯實現,更換http://www.vxbq.cn/db/時也只是修改配置文件而不用修改代碼,重用性大大提高。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈