下面是测试代码:
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;
@Before
public 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 hierarchy
2012-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 hierarchy
2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] Hibernate 3.2.5
2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] hibernate.properties not found
2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] Bytecode provider name : cglib
2012-05-15 12:24:10 [org.hibernate.cfg.Environment]-[INFO] using JDK 1.4 java.sql.Timestamp handling
2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping class: org.abin.lee.entity.Department -> department
2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping class: org.abin.lee.entity.Employee -> employee
2012-05-15 12:24:11 [org.hibernate.cfg.HbmBinder]-[INFO] Mapping collection: org.abin.lee.entity.Department.employee -> employee
2012-05-15 12:24:11 [org.springframework.orm.hibernate3.LocalSessionFactoryBean]-[INFO] Building new Hibernate SessionFactory
2012-05-15 12:24:11 [org.hibernate.connection.ConnectionProviderFactory]-[INFO] Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] RDBMS: MySQL, version: 5.5.15
2012-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.MySQLDialect
2012-05-15 12:24:11 [org.hibernate.transaction.TransactionFactoryFactory]-[INFO] Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
2012-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(): disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Automatic session close at end of transaction: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC batch size: 15
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC batch updates for versioned data: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Scrollable result sets: enabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] JDBC3 getGeneratedKeys(): enabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Connection release mode: after_transaction
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Maximum outer join fetch depth: 2
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Default batch fetch size: 1
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Generate SQL with comments: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Order SQL updates by primary key: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Order SQL inserts for batching: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2012-05-15 12:24:11 [org.hibernate.hql.ast.ASTQueryTranslatorFactory]-[INFO] Using ASTQueryTranslatorFactory
2012-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: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Second-level cache: enabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query cache: enabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Cache provider: org.hibernate.cache.EhCacheProvider
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Optimize cache for minimal puts: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Structured second-level cache entries: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Query cache factory: org.hibernate.cache.StandardQueryCacheFactory
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Echoing all SQL to stdout
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Statistics: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Deleted entity synthetic identifier rollback: disabled
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Default entity-mode: pojo
2012-05-15 12:24:11 [org.hibernate.cfg.SettingsFactory]-[INFO] Named query checking : enabled
2012-05-15 12:24:11 [org.hibernate.impl.SessionFactoryImpl]-[INFO] building session factory
2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[DEBUG] instantiating cache region: org.abin.lee.entity.Employee usage strategy: read-only
2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[WARN] read-only cache configured for mutable class: org.abin.lee.entity.Employee
2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[DEBUG] instantiating cache region: org.abin.lee.entity.Department usage strategy: read-only
2012-05-15 12:24:12 [org.hibernate.cache.CacheFactory]-[WARN] read-only cache configured for mutable class: org.abin.lee.entity.Department
2012-05-15 12:24:12 [org.hibernate.impl.SessionFactoryObjectFactory]-[INFO] Not binding factory to JNDI, no JNDI name configured
2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] Running hbm2ddl schema update
2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] fetching database metadata
2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[INFO] updating schema
2012-05-15 12:24:12 [org.hibernate.tool.hbm2ddl.TableMetadata]-[INFO] table found: abin.department
2012-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.employee
2012-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 complete
2012-05-15 12:24:12 [org.hibernate.cache.UpdateTimestampsCache]-[INFO] starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache
2012-05-15 12:24:12 [org.hibernate.cache.StandardQueryCache]-[INFO] starting query cache at region: org.hibernate.cache.StandardQueryCache
2012-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.StandardQueryCache
2012-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 null
2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] query results were not found in cache
Hibernate:
select
department0_.did as did0_,
department0_.dname as dname0_
from
abin.department department0_
order by
department0_.did desc
2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-422858128358
2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318
Hibernate:
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-8027469a1bac
2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#924fba4c-3a9c-4f33-9e09-63e49663f9a1
Hibernate:
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-47056ba8f354
2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#e789a510-2f80-4b4b-b25e-e7e48e4869da
2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5476580773695488
did=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.StandardQueryCache
2012-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.department
2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] Element for abin.department is null
2012-05-15 12:24:13 [org.hibernate.cache.StandardQueryCache]-[DEBUG] returning cached query results
2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-422858128358
2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Cache hit: org.abin.lee.entity.Department#fde9fe25-ea81-4168-b9bc-422858128358
Hibernate:
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-47056ba8f354
2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#e789a510-2f80-4b4b-b25e-e7e48e4869da
2012-05-15 12:24:13 [org.hibernate.cache.EhCache]-[DEBUG] key: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318
2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Cache hit: org.abin.lee.entity.Department#15d95f71-f41b-4369-a1ae-441366f57318
Hibernate:
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-8027469a1bac
2012-05-15 12:24:13 [org.hibernate.cache.ReadOnlyCache]-[DEBUG] Caching: org.abin.lee.entity.Employee#924fba4c-3a9c-4f33-9e09-63e49663f9a1
did=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是一对多的关系。