Hibernate查询返存

查询缓存是针对普通属性结果集的缓存,对实体对象的结果集只缓存id

查询缓存的生命周期,当前关联的表发生修改,那么查询缓存生命周期结束

查询缓存的配置和使用:

    * 在hibernate.cfg.xml文件中启用查询缓存,如:

    <property name="hibernate.cache.use_query_cache">true</property>

    * 在程序中必须手动启用查询缓存,如:

    query.setCacheable(true);

下面举例说明一下查询缓存的使用:

/**

     * 开启查询缓存,关闭二级缓存

     * 开启一个session,分别调用query.list

     */

    public void testCache1() {

        Session session = null;

        try {

            session = HibernateUtils.getSession();

            session.beginTransaction();

            

            Query query = session.createQuery("select s.name from Student s");

            //启用查询查询缓存

            query.setCacheable(true);

            

            List names = query.list(); 

            for (Iterator iter=names.iterator();iter.hasNext(); ) {

                String name = (String)iter.next();

                System.out.println(name);

            }

            

            System.out.println("-------------------------------------");

            query = session.createQuery("select s.name from Student s");

            //启用查询查询缓存

            query.setCacheable(true);

            

            //没有发出查询sql,因为启用了查询缓存

            names = query.list(); 

            for (Iterator iter=names.iterator();iter.hasNext(); ) {

                String name = (String)iter.next();

                System.out.println(name);

            }

            session.getTransaction().commit();

        }catch(Exception e) {

            e.printStackTrace();

            session.getTransaction().rollback();

        }finally {

            HibernateUtils.closeSession(session);

        }

    }    

    

    /**

     * 开启查询缓存,关闭二级缓存

     * 开启两个session,分别调用query.list

     */

    public void testCache2() {

        Session session = null;

        try {

            session = HibernateUtils.getSession();

            session.beginTransaction();

            

            Query query = session.createQuery("select s.name from Student s");

            //启用查询查询缓存

            query.setCacheable(true);

            

            List names = query.list(); 

            for (Iterator iter=names.iterator();iter.hasNext(); ) {

                String name = (String)iter.next();

                System.out.println(name);

            }

            session.getTransaction().commit();

        }catch(Exception e) {

            e.printStackTrace();

            session.getTransaction().rollback();

        }finally {

            HibernateUtils.closeSession(session);

        }

        

        System.out.println("-------------------------------------");

        

        try {

            session = HibernateUtils.getSession();

            session.beginTransaction();

            

            Query query = session.createQuery("select s.name from Student s");

            //启用查询查询缓存

            query.setCacheable(true);

            

            //不会发出查询sql,因为查询缓存的生命周期和session无关

            List names = query.list(); 

            for (Iterator iter=names.iterator();iter.hasNext(); ) {

                String name = (String)iter.next();

                System.out.println(name);

            }

            session.getTransaction().commit();

        }catch(Exception e) {

            e.printStackTrace();

            session.getTransaction().rollback();

        }finally {

            HibernateUtils.closeSession(session);

        }

    }        

/**

     * 开启查询缓存,关闭二级缓存

     * 开启两个session,分别调用query.iterate

     */

    public void testCache3() {

        Session session = null;

        try {

            session = HibernateUtils.getSession();

            session.beginTransaction();

            

            Query query = session.createQuery("select s.name from Student s");

            //启用查询查询缓存

            query.setCacheable(true);

             

            for (Iterator iter=query.iterate();iter.hasNext(); ) {

                String name = (String)iter.next();

                System.out.println(name);

            }

            session.getTransaction().commit();

        }catch(Exception e) {

            e.printStackTrace();

            session.getTransaction().rollback();

        }finally {

            HibernateUtils.closeSession(session);

        }

        

        System.out.println("-------------------------------------");

        

        try {

            session = HibernateUtils.getSession();

            session.beginTransaction();

            

            Query query = session.createQuery("select s.name from Student s");

            //启用查询查询缓存

            query.setCacheable(true);

             

            //查询缓存只对query.list()起作用,query.iterate不起作用,也就是query.iterate不使用

            //查询缓存

            for (Iterator iter=query.iterate();iter.hasNext(); ) {

                String name = (String)iter.next();

                System.out.println(name);

            }

            session.getTransaction().commit();

        }catch(Exception e) {

            e.printStackTrace();

            session.getTransaction().rollback();

        }finally {

            HibernateUtils.closeSession(session);

        }

    }

相关推荐