hibernate查询是否使用了ehcache的缓存了?

问题描述

下面是测试代码:package org.abin.lee.test;import java.util.List;import junit.framework.TestCase;import org.abin.lee.entity.Department;import org.abin.lee.service.UserService;import org.junit.Before;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestQueryEhCache extends TestCase {public UserService userService;private ApplicationContext context;@Beforepublic void setUp() {context = new ClassPathXmlApplicationContext(new String[] {"org/abin/lee/spring/applicationContext-resource.xml","org/abin/lee/spring/applicationContext-service.xml","org/abin/lee/spring/applicationContext-dao.xml" });}public void testQueryEhCache() {userService = (UserService) this.context.getBean("userService");List<Department> list=null;System.out.println("下面是执行第一次查询");try {list= this.userService.findAllDepartment();} catch (Exception e) {e.printStackTrace();}for(Department department:list){System.out.println("did="+department.getDid()+",dname="+department.getDname()+",employee="+department.getEmployee());}System.out.println("下面是执行第二次查询");try {list= this.userService.findAllDepartment();} catch (Exception e) {e.printStackTrace();}for(Department department:list){System.out.println("did="+department.getDid()+",dname="+department.getDname()+",employee="+department.getEmployee());}}public UserService getUserService() {return userService;}public void setUserService(UserService userService) {this.userService = userService;}}下面是控制台的打印信息:2012-05-15 12:24:09 [org.springframework.context.support.ClassPathXmlApplicationContext]-[INFO] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a0dcd9: startup date [Tue May 15 12:24:09 CST 2012]; root of context hierarchy2012-05-15 12:24:09 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [org/abin/lee/spring/applicationContext-resource.xml]2012-05-15 12:24:10 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [org/abin/lee/spring/applicationContext-service.xml]2012-05-15 12:24:10 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [org/abin/lee/spring/applicationContext-dao.xml]2012-05-15 12:24:10 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[INFO] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1362012: defining beans [dataSource,sessionFactory,hibernateTemplate,transactionManager,transactionInterceptor,org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator#0,userService,userDao]; root of factory hierarchy2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] Hibernate 3.2.52012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] hibernate.properties not found2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] Bytecode provider name : cglib2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] using JDK 1.4 java.sql.Timestamp handling2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping class: org.abin.lee.entity.Department -> department2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping class: org.abin.lee.entity.Employee -> employee2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping collection: org.abin.lee.entity.Department.employee -> employee2012-05-15 12:24:11 [org.springframework.orm.hibernate3.LocalSessionFactoryBean]-[INFO] Building new Hibernate SessionFactory2012-05-15 12:24:11 [org.hibernate.connection.ConnectionProviderFactory]-[INFO] Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] RDBMS: MySQL, version: 5.5.152012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.0-beta ( $Date: 2005-11-17 16:14:47 +0100 (Thu, 17 Nov 2005) $, $Revision$ )2012-05-15 12:24:11 [org.hibernate.dialect.Dialect]-[INFO] Using dialect: org.hibernate.dialect.MySQLDialect2012-05-15 12:24:11 [org.hibernate.transaction.TransactionFactoryFactory]-[INFO] Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory2012-05-15 12:24:11 [org.hibernate.transaction.TransactionManagerLookupFactory]-[INFO] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Automatic flush during beforeCompletion(): disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Automatic session close at end of transaction: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC batch size: 152012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC batch updates for versioned data: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Scrollable result sets: enabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC3 getGeneratedKeys(): enabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Connection release mode: after_transaction2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Maximum outer join fetch depth: 22012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Default batch fetch size: 12012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Generate SQL with comments: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Order SQL updates by primary key: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Order SQL inserts for batching: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory2012-05-15 12:24:11 [org.hibernate.hql.ast.ASTQueryTranslatorFactory]-[INFO] Using ASTQueryTranslatorFactory2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query language substitutions: {}2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JPA-QL strict compliance: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Second-level cache: enabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query cache: enabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Cache provider: org.hibernate.cache.EhCacheProvider2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Optimize cache for minimal puts: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Structured second-level cache entries: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query cache factory: org.hibernate.cache.StandardQueryCacheFactory2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Echoing all SQL to stdout2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Statistics: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Deleted entity synthetic identifier rollback: disabled2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Default entity-mode: pojo2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Named query checking : enabled2012-05-15 12:24:11 [org.hibernate.impl.SessionFactoryImpl]-[INFO] building session factory2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[DEBUG] instantiating cache region: org.abin.lee.entity.Employee usage strategy: read-only2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[WARN] read-only cache configured for mutable class: org.abin.lee.entity.Employee2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[DEBUG] instantiating cache region: org.abin.lee.entity.Department usage strategy: read-only2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[WARN] read-only cache configured for mutable class: org.abin.lee.entity.Department2012-05-15 12:24:12 [org.hibernate.impl.SessionFactoryObjectFactory]-[INFO] Not binding factory to JNDI, no JNDI name configured2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] Running hbm2ddl schema update2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] fetching database metadata2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] updating schema2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] table found: abin.department2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] columns: [dname, did]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] foreign keys: []2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] indexes: [primary]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] table found: abin.employee2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] columns: [ename, did, eid]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] foreign keys: [fdid, fk4722e6aefea415dc]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] indexes: [primary, fk4722e6aefea415dc]2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] schema update complete2012-05-15 12:24:12 [org.hibernate.cache.UpdateTimestampsCache]-[INFO] starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache2012-05-15 12:24:12 [org.hibernate.cache.StandardQueryCache]-[INFO] starting query cache at region: org.hibernate.cache.StandardQueryCache2012-05-15 12:24:12 [org.springframework.orm.hibernate3.HibernateTransactionManager]-[INFO] Using DataSource [org.apache.commons.dbcp.BasicDataSource@1fa39bb] of Hibernate SessionFactory for HibernateTransactionManager下面是执行第一次查询2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] checking cached query results in region: org.hibernate.cache.StandardQueryCache2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: sql: select department0_.did as did0_, department0_.dname as dname0_ from abin.department department0_ order by department0_.did desc; parameters: ; named parameters: {}2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] Element for sql: select department0_.did as did0_, department0_.dname as dname0_ from abin.department department0_ order by department0_.did desc; parameters: ; named parameters: {} is null2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] query results were not found in cacheHibernate: select department0_.did as did0_, department0_.dname as dname0_ from abin.department department0_ order by department0_.did desc2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-4228581283582012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#788d9a5a-9085-4df9-a058-8027469a1bac2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#924fba4c-3a9c-4f33-9e09-63e49663f9a1Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#291e9165-dcc2-4648-bb05-47056ba8f3542012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#e789a510-2f80-4b4b-b25e-e7e48e4869da2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5476580773695488did=fde9fe25-ea81-4168-b9bc-422858128358,dname=department2,employee=[org.abin.lee.entity.Employee@5c98f3, org.abin.lee.entity.Employee@bef361]did=15d95f71-f41b-4369-a1ae-441366f57318,dname=department1,employee=[org.abin.lee.entity.Employee@e0e515, org.abin.lee.entity.Employee@10dc656]下面是执行第二次查询2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] checking cached query results in region: org.hibernate.cache.StandardQueryCache2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: sql: select department0_.did as did0_, department0_.dname as dname0_ from abin.department department0_ order by department0_.did desc; parameters: ; named parameters: {}2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] Checking query spaces for up-to-dateness: [abin.department]2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: abin.department2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] Element for abin.department is null2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] returning cached query results2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-4228581283582012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Cache hit: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-422858128358Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#291e9165-dcc2-4648-bb05-47056ba8f3542012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#e789a510-2f80-4b4b-b25e-e7e48e4869da2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f573182012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Cache hit: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#788d9a5a-9085-4df9-a058-8027469a1bac2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#924fba4c-3a9c-4f33-9e09-63e49663f9a1did=fde9fe25-ea81-4168-b9bc-422858128358,dname=department2,employee=[org.abin.lee.entity.Employee@ff9053, org.abin.lee.entity.Employee@5c7734]did=15d95f71-f41b-4369-a1ae-441366f57318,dname=department1,employee=[org.abin.lee.entity.Employee@1000bcf, org.abin.lee.entity.Employee@754fc]我想知道一下,第二次查询是不是使用了hibernate的ehcache查询了?这里的Department和Employee是一对多的关系。 问题补充:jobar 写道

解决方案

引用是不是这个 key: 和Cache hit:??? 是的,cache hit就是缓存命中的意思
解决方案二:
从日志来看,确实是使用了ecache缓存,并且第二次查询从缓存命中了查询结果直接返回2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f573182012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Cache hit: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318Hibernate: select employee0_.did as did1_, employee0_.eid as eid1_, employee0_.eid as eid1_0_, employee0_.ename as ename1_0_, employee0_.did as did1_0_ from abin.employee employee0_ where employee0_.did=?

时间: 2024-09-18 21:21:05

hibernate查询是否使用了ehcache的缓存了?的相关文章

hibernate 缓存-hibernate查询数据库与缓存开启时机

问题描述 hibernate查询数据库与缓存开启时机 关于hibernate在第一次查询数据库的时候,是否开启缓存,及开启缓存的时机,使用的缓存是以及还是二级,请详情说明下,万分感谢!!!

设计-关于hibernate查询的和开发中的一些疑问

问题描述 关于hibernate查询的和开发中的一些疑问 我在开发的过程中遇到一些疑问,希望各大牛帮我解答下. 环境介绍: 假设现在有2个表 表名:category(文章分类) 字段: id 主键 name 文章分类中文名 desc 描述 表名:article(文章列表) 字段: id 主键 categoryid 分类id title 文章标题 content 内容 因为用的是hibernate, 上面两个表对应的PO分别是CategoryPO , ArticlePO.没有配置外键映射. 现在要

细谈Ehcache页面缓存的使用

/** *作者:张荣华 *日期:2007-9-30 **/ 关于缓存的话题,在坛子里已经有很多讨论,简单的来说,如果一个应用中80%的时间内都在访问20%的数据,那么,这时候就应该使用缓存了.这个和长尾理论正好相悖,其实也不是相悖,只是不同的理论使用的场景不同.在80/20原则生效的地方,我们都应该考虑是否可以使用缓存.但即使是这样,缓存也有不同的用法,举个例子,一个网站的首页估计是被访问的次数最多的,我们可以考虑给首页做一个页面缓存,而如果在某个页面上,比如说javaeye的java版区只有前

Spring+Ehcache怎么缓存List集合?

问题描述 Spring+Ehcache怎么缓存List集合? Spring+Ehcache怎么实现缓存List集合?...................................... 解决方案 如何在JPA,Hibernate 和 Spring 中配置 Ehcache 缓存 解决方案二: http://mixer-b.iteye.com/blog/1563872

用hibernate查询一段时间的记录

这是在一个群里讨论的问题. 方法很多:如果说数据库中数据的存储是用date类型的话,改怎么做,如果说做数据迁移的话又该怎么做? 最终的讨论方案在用字符串存储格式上比较好,毕竟各个数据库对时间的存储存在差异. 一下是我用hibernate中的HQL,和QBC两种检索方式对一段时间内的数据进行检索. 1 package com.duduli.li; 2 3 import java.util.Date; 4 import java.util.Iterator; 5 import java.util.L

hibernate查询速度问题

问题描述 用hibernate查询数据,记录1000条,一次查出,不分页,从按查询到显示在jsp需要10几秒,请问是否正常?问题补充:要求不分页的,直接显示1000条 解决方案 应该不是慢在查询上面了,应该是查询的1000条记录放在List之中,以及把List之中的objcet显示给页面速度慢.你可以试一下直接在数据库中查询这些记录 看看速度多少,如果直接查1~2s Hibernate查也就在4s 应该用不了. 我的意思是 Hibernate不比直接SQL查慢多少.解决方案二:查询有没有表连接.

java-jdbc 来查询视图 和 hibernate 查询视图 问题

问题描述 jdbc 来查询视图 和 hibernate 查询视图 问题 伙伴们 现在我在用 jdbc 查询视图 和 hibernate查询视图 都显示不出来数据 (list用来接受数据,调试时的list的size不为0,可数据都是Null 这是怎么回事) 解决方案 JPA的视图查询Hibernate查询视图数据Hibernate createQuery查询视图返回list为空 解决方案二: 如果你给一下查询的代码,或许可以帮你分析一下

hibernate查询两张表问题,求解答?

问题描述 hibernate查询两张表问题,求解答? 我有 user表 : 有name,age,等等 course表: coursename,score,teachername,user_ID(user主键) 现在我需要前台展示一个数据表 学生姓名 课程 分数 应该怎么实现啊?使用链表吗? ?? 解决方案 最好是在数据库把这几张表做一个视图,然后写一个model类对应这个视图,这样更简单方便点 解决方案二: 如果这2张表有关联的话,,hibernate会自动级联的,查user就会把course查

一个hibernate查询oracle 8i 的问题

问题描述 一个hibernate查询oracle8i的问题:在dao中用sql查询某个日期(包含分.秒),sql如:selectid,record_datefromreftableoracle8i的retable表为:(idvarchar2(30);record_datedate).表数据为:('ref123','2010-02-2010:20:36')现在的问题是:用SqlQuery查询上面的sql后的结果中,record_date值只有年月日,没有分秒,怎样写sql才能查询出record_d