1>查询缓存的启用不但要在配置文件中进行配置
<property name="hibernate.cache.use_query_cache">true</property>
2>还要在程序中显示的进行启用
query.setCacheable(true);
private Integer id;
private String name;
//一系列的setter.getter方法
}
<!-- 指定本类的对象使用二级缓存(这也可以放在hibernate.cfg.xml中统一指定) -->
<!--
<cache usage="read-only"/>
-->
<id name="id" length="4">
<generator class="native"></generator>
</id>
<property name="name" length="10"></property>
</class>
<session-factory>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL10</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">yf123</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.show_sql">true</property>
<!-- 开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 指定二级缓存产品的提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 启用查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<mapping resource="com/sxt/hibernate/cache/entity/Student.hbm.xml"/>
<!-- 指定那些类使用二级缓存 -->
<class-cache usage="read-only" class="com.sxt.hibernate.cache.entity.Student"/>
</session-factory>
</hibernate-configuration>
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启一个session,分别调用query.list
*/
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
/*
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
System.out.println("================================");
query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
//没有发出查询语句,因为这里使用的查询缓存
names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.list
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
//query.setCacheable(true);
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
//query.setCacheable(true);
//不会发出查询语句,因为查询缓存和session无关.
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.iterate
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
for (Iterator<String> it = query.iterate(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
//会发出查询语句,因为query.iterate不使用查询缓存
for (Iterator<String> it = query.iterate(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 关闭查询缓存,关闭二级缓存, 开启两个session,分别调用query.list查询实体对象
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
//query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
//query.setCacheable(true);
//会发出查询语句,因为list默认每次都会发出sql语句
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.list查询实体对象
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
//会发出根据id查询实体的n条查询语句,因为这种情况下,查询过程是这样的:
// 在第一次执行list时,会把查询对象的id缓存到查询缓存里
// 第二次执行list时, 会遍历查询缓存里的id到缓存里去找实体对象,由于这里没找到实体对象,
//所以就发出n条查询语句到数据库中查询.
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
/**
* 开启查询缓存,开启二级缓存, 开启两个session,分别调用query.list查询实体对象
*/
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
//不会发出查询语句,因为这种情况下,查询过程是这样的:
// 在第一次执行list时,会把查询对象的id缓存到查询缓存里
// 第二次执行list时, 会遍历查询缓存里的id到缓存里去找实体对象,由于这里开启了二级缓存,可以找到目标实体对象,
//所以就不会再发出n条查询语句.
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
1089




被折叠的 条评论
为什么被折叠?



