Hibernate的二级缓存 下

表positionContent

HibernateTest.java

id

代码:

position_id

/**
 *
 */
package com.b510.examplex;

content_title

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

content_id

/**
 *
 * @author XHW
 *
 * @date 2011-7-15
 *
 */
public class HibernateTest {

is_recommend

 /**
  * @param args
  */
 public static void main(String[] args) {
  new HibernateTest().getGuestbooks();
 }

list_sort

 public void getGuestbooks(){
  SessionFactory
sessionFactory=HibernateSessionFactoryUtil.getSessionFactory();
  Session session1=sessionFactory.getCurrentSession();
  System.out.println("Session1:"+session1);
  Transaction tx=session1.beginTransaction();

update_time

//Guestbook
gb=(Guestbook)session1.load(Guestbook.class, 1);
  Guestbook gb=(Guestbook)session1.get(Guestbook.class, 1);
  System.out.println("ID : "+gb.getId()+"name:  "+gb.getName());
  tx.commit();
  
  System.out.println("----------------------------------");
  
  Session session2=sessionFactory.getCurrentSession();
  System.out.println("Session2:"+session2);
  System.out.println("session1是否等于session2  ?");
  System.out.println(session1==session2);
  session2.beginTransaction();

create_time

//  gb=(Guestbook)session2.load(Guestbook.class,
1);      load和get方法在这里可以随便使用,不会影响运行的结果
  gb=(Guestbook)session2.get(Guestbook.class, 1);
  System.out.println("ID : "+gb.getId()+"name:  "+gb.getName());
  session2.getTransaction().commit();
 } 
}

10 14 大成成长 160910 1 1 2017-02-09 11:51:56 2017-02-09 11:51:56 11 15
创新成长 160910 1 1 2017-02-09 11:52:08 2017-02-09 11:52:08

运行效果:

id

log4j:WARN No appenders could be found for logger
(org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Session1:SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[]
updates=[] deletions=[] collectionCreations=[]
collectionRemovals=[] collectionUpdates=[]])
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=?

name

ID : 1name:  liuwei

Session2:SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[]
updates=[] deletions=[] collectionCreations=[]
collectionRemovals=[] collectionUpdates=[]])
session1是否等于session2  ?
false
ID : 1name:  liuwei

我们看到,session1和session2两个session对象是不想等的,也就是说两个session的地址不是同一个内存地址

然而我们却看到,这里却只用了一条select语句,我们明明是查询两条记录啊!这说明HIbernate的缓存是跨session的

也就是一个session对象被干掉了以后,他的缓存还是存在的,不因session被干掉二干掉。这样在我们读取相同的数据

的时候就不会再却读数据库,而是直接从缓存中读取出来。

测试代码二:

HibernateTest.java

代码;

/**
 *
 */
package com.b510.examplex;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author XHW
 *
 * @date 2011-7-15
 *
 */
public class HibernateTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  new HibernateTest().getGuestbooks();
 }

 public void getGuestbooks(){
  SessionFactory
sessionFactory=HibernateSessionFactoryUtil.getSessionFactory();
  Session session1=sessionFactory.getCurrentSession();
  Transaction tx=session1.beginTransaction();
  Query query=session1.createQuery("from
Guestbook");
  List<Guestbook>
list=query.list();
  for(Guestbook gb:list){
   System.out.println("ID :
"+gb.getId()+"name:  "+gb.getName());
  }
  tx.commit();
  
  System.out.println("----------------------------------");
  
  Session session2=sessionFactory.getCurrentSession();
  session2.beginTransaction();
  query=session2.createQuery("from
Guestbook");
  list=query.list();
  for(Guestbook gb:list){
   System.out.println("ID :
"+gb.getId()+"name:  "+gb.getName());
  }
  session2.getTransaction().commit();
 } 
}

运行效果:

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_,
        guestbook0_.version as version0_,
        guestbook0_.name as name0_,
        guestbook0_.email as email0_,
        guestbook0_.phone as phone0_,
新京葡娱乐场网址,        guestbook0_.title as title0_,
        guestbook0_.content as content0_,
        guestbook0_.created_time as created8_0_
    from
        users.guestbook guestbook0_
ID : 1name:  liuwei
ID : 2name:  Hongtenzone@foxmail.com
ID : 3name:  Hongten
ID : 4name:  HOngten
ID : 5name:  HOngten
ID : 6name:  HOngten

title

ID : 7name:  HOngten

Hibernate:
    select
        guestbook0_.id as id0_,
        guestbook0_.version as version0_,
        guestbook0_.name as name0_,
        guestbook0_.email as email0_,
        guestbook0_.phone as phone0_,
        guestbook0_.title as title0_,
        guestbook0_.content as content0_,
        guestbook0_.created_time as created8_0_
    from
        users.guestbook guestbook0_
ID : 1name:  liuwei
ID : 2name:  Hongtenzone@foxmail.com
ID : 3name:  Hongten
ID : 4name:  HOngten
ID : 5name:  HOngten
ID : 6name:  HOngten
ID : 7name:  HOngten

我们看到的结果是用两条select语句,查出了相同的结果!

这个list对象有这么一个特点:他只是从数据库中读取数据,放到缓存中,但是据对不会到缓存中

去索要数据。也就是说list只是贡献缓存,不会索要缓存。

他不用二级缓存。

测试代码三:

HibernateTest.java

代码:

/**
 *
 */
package com.b510.examplex;

import java.util.List;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author XHW
 *
 * @date 2011-7-15
 *
 */
public class HibernateTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  new HibernateTest().getGuestbooks();
 }

 public void getGuestbooks(){
  SessionFactory
sessionFactory=HibernateSessionFactoryUtil.getSessionFactory();
  Session session1=sessionFactory.getCurrentSession();
  Transaction tx=session1.beginTransaction();
  Query query=session1.createQuery("from Guestbook");
  List<Guestbook>
list=query.list();
  for(Guestbook gb:list){
   System.out.println("ID :
"+gb.getId()+"name:  "+gb.getName());
  }
  tx.commit();
  
  System.out.println("----------------------------------");
  
  Session session2=sessionFactory.getCurrentSession();
  session2.beginTransaction();
  query=session2.createQuery("from Guestbook");
  Iterator it=query.iterate();
  while(it.hasNext()){
   Guestbook
gb=(Guestbook)it.next();
   System.out.println("ID :
"+gb.getId()+"name:  "+gb.getName());
  }
  session2.getTransaction().commit();
 } 
}

运行效果:

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_,
        guestbook0_.version as version0_,
        guestbook0_.name as name0_,
        guestbook0_.email as email0_,
        guestbook0_.phone as phone0_,
        guestbook0_.title as title0_,
        guestbook0_.content as content0_,
        guestbook0_.created_time as created8_0_
    from
        users.guestbook guestbook0_
ID : 1name:  liuwei
ID : 2name:  Hongtenzone@foxmail.com
ID : 3name:  Hongten
ID : 4name:  HOngten
ID : 5name:  HOngten
ID : 6name:  HOngten

type

ID : 7name:  HOngten

Hibernate:
    select
        guestbook0_.id as col_0_0_
    from
        users.guestbook guestbook0_
ID : 1name:  liuwei
ID : 2name:  Hongtenzone@foxmail.com
ID : 3name:  Hongten
ID : 4name:  HOngten
ID : 5name:  HOngten
ID : 6name:  HOngten
ID : 7name:  HOngten

第一条selelct语句是从数据库中读出数据然后将数据放入二级缓存中,接着进入第二个会话

Iterator会向二级缓存索要查询的数据,他的方式是首先将要查的实体类的主键值获取到,

这样在底层上就要使用一条selelct  id
的语句,就是我们看到的第二条selelct语句。这样就获取了

这个持久化对象的的主键值的一个集合。从
这个主键值集合中再分别读出一个主键值,将他的其他内容

读取出来。如上面显示的一样,先获取Id=1,然后获取id=1的其他数据,如name字段的值,然后再获取

id=2,再获取id=2的其他数据(name),以这种方式进行下去,直到读取完数据。还要申明的是,Iterator

都是先从二级缓存中获取数据,如果不在二级缓存中,那么要使用“n+1”方式了。

看下面的测试代码:

HibernateTest.java

代码:

/**
 *
 */
package com.b510.examplex;

import java.util.List;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author XHW
 *
 * @date 2011-7-15
 *
 */
public class HibernateTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  new HibernateTest().getGuestbooks();
 }

 public void getGuestbooks(){
  SessionFactory
sessionFactory=HibernateSessionFactoryUtil.getSessionFactory();
  
  /*
  Session
session1=sessionFactory.getCurrentSession();
  Transaction
tx=session1.beginTransaction();
  Query query=session1.createQuery("from
Guestbook");
  List<Guestbook>
list=query.list();
  for(Guestbook gb:list){
   System.out.println("ID :
"+gb.getId()+"name:  "+gb.getName());
  }
  tx.commit();
  */
  System.out.println("----------------------------------");
  
  Session session2=sessionFactory.getCurrentSession();
  session2.beginTransaction();
  Query query=session2.createQuery("from Guestbook");
  Iterator it=query.iterate();
  while(it.hasNext()){
   Guestbook gb=(Guestbook)it.next();
   System.out.println("ID : "+gb.getId()+"name:  "+gb.getName());
  }
  session2.getTransaction().commit();
 } 
}

运行效果:

log4j:WARN No appenders could be found for logger
(org.hibernate.cfg.Environment).

相关文章

Comment ()
评论是一种美德,说点什么吧,否则我会恨你的。。。