【Hibernate】持久化对象的状态的变化和对应的方法

来源:互联网 发布:手机载图软件 编辑:程序博客网 时间:2024/05/21 17:07

一)持久化对象的状态变化和对应的方法

(1)持久化对象在整个hibernate框架中运行,一共有四种不同的状态产生:
>>临时状态对象:
 A)在session一级缓存之【外】
          B)【没有】与数据库交互的能力
          C)hibernate【不会】为临时状态对象分配oid值  
>>持久化状态对象 :
 A)在session一级缓存之【内】
          B)【有】与数据库交互的能力,即CURD
          C)hibernate【会】为持久化状态对象分配oid值  

>>游离状态对象:
 A)在session一级缓存之【外】
          B)【没有】与数据库交互的能力
          C)hibernate依然存在【已分配】oid值  
>>删除状态对象
 A)在session一级缓存之【外】
          B)【没有】与数据库交互的能力
          C)hibernate依然存在【已分配】oid值  
>>GC在适当的时候回收删除状态对象,但是GC在特定的的时候,也会回收:临时状态对象,游离状态对象.

CURD方法使用的细节:
>>持久化状态对象,不能同时出现二个相同的OID值.
        >>当持久化状态对象在session一级缓中,就必须有一条记录与之一一对应.
        >>你可以使用saveOrUpdate()去替换save()和update()方法 .
          这时saveOrUpdate(临时状态对象)的话,将执行save()操作.
          这时saveOrUpdate(游离状态对象)的话,将执行update()操作.


下面演示在session一级缓存内调用get()和load()方法的区别:

贴出javabean/实体;Customers类:

package cn.lsh.web.domain;public class Customers {private int id;private String name;private String gender;private int age;private String des;public Customers() {super();}public Customers(int id, String name, String gender, int age, String des) {super();this.id = id;this.name = name;this.gender = gender;this.age = age;this.des = des;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getDes() {return des;}public void setDes(String des) {this.des = des;}}
实体所对应的映射文件:

<!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">        <hibernate-mapping package="cn.lsh.web.domain">    <class name="Customers" table="CUSTOMERS">    <id name="id" column="ID" type="int"><generator class="increment"/></id>    <property name="name" column="NAME" type="string"></property>    <property name="gender" column="GENDER" type="string"></property>    <property name="age" column="AGE" type="int"></property>    <property name="des" column="DES" type="string"></property>    </class>    </hibernate-mapping>

假设我的数据库添加的数据为两条数据记录(那么所对应的第三条记录是不存在的):

<span style="font-size:14px;">mysql> select * from customers;+----+------+--------+------+------------------+| id | name | gender | age  | des              |+----+------+--------+------+------------------+|  1 | 刘备 | 男     |   25 | 三国杰出人物之一 ||  2 | 孙权 | 男     |   35 | 三国杰出人物之一 |+----+------+--------+------+------------------+</span>

测试:CustomerDao类(使用junit测试)

get()方法测试:

public class CustomerDao {private static SessionFactory sessionFactory;static{//加载映射文件Configuration config = new Configuration().configure();//创建session父类工厂sessionFactory = config.buildSessionFactory();}//添加@Testpublic void addCustomer(){Session session = sessionFactory.openSession();Customers c = new Customers(2,"曹操","男",30,"三国杰出人物之一");Transaction t = session.beginTransaction();try {session.save(c);t.commit();} catch (Exception e) {e.printStackTrace();t.rollback();}finally{session.close();}}
<span style="white-space:pre"></span>//查找1<span style="white-space:pre"></span>@Test<span style="white-space:pre"></span>public void findCustomer1(){<span style="white-space:pre"></span>Session session = sessionFactory.openSession();<span style="white-space:pre"></span>
<span style="white-space:pre"></span>//开启事物
<span style="white-space:pre"></span>Transaction t = session.beginTransaction();<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>
<span style="white-space:pre"></span>//使用get()方法查询数据库对应的第三条数据(数据库中第三条数据不存在)<span style="white-space:pre"></span>Customers c1 = (Customers) session.get(Customers.class,3);<span style="white-space:pre"></span>System.out.println(c1.getName()+":"+c1.getDes());<span style="white-space:pre"></span>t.commit();<span style="white-space:pre"></span>} catch (Exception e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>t.rollback();<span style="white-space:pre"></span>}finally{<span style="white-space:pre"></span>session.close();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}
结果报异常(空指针异常):java.lang.NullPointerException
at cn.lsh.web.dao.CustomerDao.findCustomer(CustomerDao.java:61)

load()方法测试:

//查找2@Testpublic void findCustomer2(){Session session = sessionFactory.openSession();Transaction t = session.beginTransaction();try {Customers c1 = (Customers) session.load(Customers.class,1);System.out.println(c1.getName()+":"+c1.getDes());t.commit();} catch (Exception e) {e.printStackTrace();t.rollback();}finally{session.close();}}}
结果报异常(对象找不到异常):org.hibernate.ObjectNotFoundException: No row with the given identifierexists[cn.lsh.web.domain.Customers#3]

总结:

1.如果get方法找不到对应的记录时,返回java.lang.NullPointerException;
   如果get方法找得到对应的记录时,会自动封装成对象,将该对象返回.

2.如果load方法找不到对应的记录时,返回org.hibernate.ObjectNotFoundException;
   如果load方法找得到对应的记录时,会自动封装成对象,将该对象返回.

0 0