Hibernate中的一级缓存

HibernateTest.java

代码:

 1 /**
 2  * 
 3  */
 4 package com.b510.examplex;
 5 
 6 import org.hibernate.Session;
 7 
 8 
 9 /**
10  *
11  * @author XHW
12  *
13  * @date 2011-7-15
14  *
15  */
16 public class HibernateTest {
17 
18  /**
19   * @param args
20   */
21  public static void main(String[] args) {
22   new HibernateTest().getGuestbooks();
23  }
24 
25  public void getGuestbooks(){
26   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
27   session.beginTransaction();
28   
29   Guestbook gb=(Guestbook)session.get(Guestbook.class, 2);
30   Guestbook gb2=(Guestbook)session.get(Guestbook.class, 2);
31   
32   System.out.println(gb==gb2);
33   session.getTransaction().commit();
34  }
35 
36 }

运行效果:

我们会看到,这里只用了一条select语句。但是我们读的是两条记录啊!

为什么会出现这种情况呢?

这是因为 Hibernate使用了一级缓存,一级缓存又叫Session缓存

在一个会话的生命期里面,他所用到的数据会使用缓冲的,第一次读的是编号为2的数据记录

第一次读的时候,内存中什么都没有,这时候就要用一条select语句从数据库中读出一条记录,同时把这些数据放到session的缓存

里面,接下来又要读同样的数据,这时候他就不会再向数据库读取数据,而是向缓存中读取数据。

得到这个对象,把这个对象返回,所以我们看到一条select语句

Sysout(true);即:gb=gb2

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
true

但是,如果我们在session对象结束的时候再来sysout(gb=gb2),

会出现什么样的结果呢?

我们看看下面的运行效果就会看到,这时候会看到两条select语句,

一个session对象结束后,这是缓存中的数据会清空。

所以我们看到两条select语句。

代码:

 1 /**
 2  * 
 3  */
 4 package com.b510.examplex;
 5 
 6 import org.hibernate.Session;
 7 
 8 
 9 /**
10  *
11  * @author XHW
12  *
13  * @date 2011-7-15
14  *
15  */
16 public class HibernateTest {
17 
18  /**
19   * @param args
20   */
21  public static void main(String[] args) {
22   new HibernateTest().getGuestbooks();
23  }
24 
25  public void getGuestbooks(){
26   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
27   session.beginTransaction();
28   
29   Guestbook gb=(Guestbook)session.get(Guestbook.class, 2);   
30   session.getTransaction().commit();
31 
32   session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
33   session.beginTransaction(); 
34   
35   Guestbook gb2=(Guestbook)session.get(Guestbook.class, 2);    
36   session.getTransaction().commit();
37   
38   System.out.println(gb==gb2);
39  }
40 
41 }

运行效果:

我们看到,我们要跟新两条记录,但是这里只是执行了一条update语句。

这是Hibernate利用缓存,用一个update就可以了。

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
false

 HibernateTest.java

 1 /**
 2  * 
 3  */
 4 package com.b510.examplex;
 5 
 6 import org.hibernate.Session;
 7 
 8 
 9 /**
10  *
11  * @author XHW
12  *
13  * @date 2011-7-15
14  *
15  */
16 public class HibernateTest {
17 
18  /**
19   * @param args
20   */
21  public static void main(String[] args) {
22   new HibernateTest().getGuestbooks();
23  }
24 
25  public void getGuestbooks(){
26   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
27   session.beginTransaction();
28   
29   Guestbook gb=(Guestbook)session.get(Guestbook.class, 2);
30   Guestbook gb2=(Guestbook)session.get(Guestbook.class, 2);
31    
32   gb.setName("Hongtenzone@foxmail.com");
33   gb2.setEmail("Helloworld@sina.com");
34   session.getTransaction().commit();
35 
36  }
37 
38 }

运行效果:

 

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
Hibernate: 
    update
        users.guestbook 
    set
        version=?,
        name=?,
        email=?,
        phone=?,
        title=?,
        content=?,
        created_time=? 
    where
        id=? 
        and version=?

HibernateTest.java

代码:

 1 /**
 2  * 
 3  */
 4 package com.b510.examplex;
 5 
 6 import org.hibernate.Session;
 7 
 8 
 9 /**
10  *
11  * @author XHW
12  *
13  * @date 2011-7-15
14  *
15  */
16 public class HibernateTest {
17 
18  /**
19   * @param args
20   */
21  public static void main(String[] args) {
22   new HibernateTest().getGuestbooks();
23  }
24 
25  public void getGuestbooks(){
26   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
27   session.beginTransaction();
28   
29   Guestbook gb=(Guestbook)session.get(Guestbook.class, 2);
30   //移除gb的缓存
31   session.evict(gb);  
32   Guestbook gb2=(Guestbook)session.get(Guestbook.class, 2);   
33   
34   System.out.println(gb==gb2);
35   session.getTransaction().commit();
36 
37  }
38 
39 }

运行效果:

首先查出gb对象的数据,放入缓存中,当执行session.evict(gb);语句时,就会把

缓存中的数据移除掉。

这次要查gb2对象的数据,就得从新生成一条select语句。

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
false

测试二:

HIbernateTest.java

代码:

 1 /**
 2  * 
 3  */
 4 package com.b510.examplex;
 5 
 6 import org.hibernate.Session;
 7 
 8 
 9 /**
10  *
11  * @author XHW
12  *
13  * @date 2011-7-15
14  *
15  */
16 public class HibernateTest {
17 
18  /**
19   * @param args
20   */
21  public static void main(String[] args) {
22   new HibernateTest().getGuestbooks();
23  }
24 
25  public void getGuestbooks(){
26   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
27   session.beginTransaction();
28   
29   Guestbook gb=(Guestbook)session.get(Guestbook.class, 2);
30   Guestbook gb2=(Guestbook)session.get(Guestbook.class, 3);   
31   //清楚缓存,把缓存中的所有东西都给干掉,不留下任何东西
32   session.clear();
33   Guestbook gb3=(Guestbook)session.get(Guestbook.class, 2);
34   Guestbook gb4=(Guestbook)session.get(Guestbook.class, 3); 
35   
36   System.out.println(gb==gb3);
37   System.out.println(gb2==gb4);
38   session.getTransaction().commit();
39 
40  }
41 
42 }

运行效果:

我们看到session.clear();的执行效果果然是“心狠手辣”,不让敌人有丝毫反抗的有生力量……

这个方法会让缓存中的所有东西清除的干干净净。通通的,大大的,全部干掉的……

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
false
false

测试三:

HibernateTest.java

代码:

 1 /**
 2  * 
 3  */
 4 package com.b510.examplex;
 5 
 6 import org.hibernate.Session;
 7 
 8 
 9 /**
10  *
11  * @author XHW
12  *
13  * @date 2011-7-15
14  *
15  */
16 public class HibernateTest {
17 
18  /**
19   * @param args
20   */
21  public static void main(String[] args) {
22   new HibernateTest().test();
23  }
24 
25  public void test(){
26   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
27   session.beginTransaction();
28   
29   Guestbook gb=(Guestbook)session.get(Guestbook.class, 2);
30   Guestbook gb2=(Guestbook)session.get(Guestbook.class, 3);
31   //移除干净缓存中的数据
32   session.clear(); 
33   
34   //判断gb2对象的是否在缓存中
35   System.out.println(session.contains(gb2));
36 
37 //session.flush();              session刷新
38 
39 //session.close();              session关闭
40 
41 说明一下,在session执行下面的commit()方法后,会自动调用
42 
43 flush()方法和close()方法。这两步是隐含在里面,所以不用显示出来
44 
45 都是可以的。
46   session.getTransaction().commit();
47 
48  }
49 
50 }

运行效果:

session.contains(gb2);返回的是一个boolean值,判断缓存中是否缓存在gb2对象。

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
Hibernate: 
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_ 
    from
        users.guestbook guestbook0_ 
    where
        guestbook0_.id=?
false
posted @ 2011-07-17 01:41  Hongten  阅读(1403)  评论(0编辑  收藏  举报
Fork me on GitHub