hibernate缓存
来源:互联网 发布:分类别思维知乎 编辑:程序博客网 时间:2024/05/01 14:42
一级缓存:
Hibernate一级缓存是强制的,它是Session级缓存。如果会话关闭,所有缓存的对象都将丢失。二级缓存配置步骤:
1.在指定hibernate.cfg.xml配置文件中配置缓存提供程序(这里选择EHCache作为第二级缓存提供程序):
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- Assume students is the database name --> <property name="hibernate.connection.url"> jdbc:mysql://localhost/test </property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"> root123 </property> <property name="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </property> <!-- List of XML mapping files --> <mapping resource="Employee.hbm.xml"/></session-factory></hibernate-configuration>
注:有4种缓存提供程序,兼容矩阵如下:
<diskStore path="java.io.tmpdir"/><defaultCachemaxElementsInMemory="1000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="true"/><cache name="Employee"maxElementsInMemory="500"eternal="true"timeToIdleSeconds="0"timeToLiveSeconds="0"overflowToDisk="false"/>3.应用二级缓存(4种并发策略:Transactional、Read-write、Nonstrict-read-write、Read-only):
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Employee" table="EMPLOYEE"> <meta attribute="class-description"> This class contains the employee detail. </meta> <cache usage="read-write"/> <id name="id" type="int" column="id"> <generator class="native"/> </id> <property name="firstName" column="first_name" type="string"/> <property name="lastName" column="last_name" type="string"/> <property name="salary" column="salary" type="int"/> </class></hibernate-mapping>
查询级别缓存:
必须先使用 hibernate.cache.use_query_cache="true"属性在配置文件中激活它。Session session = SessionFactory.openSession();Query query = session.createQuery("FROM EMPLOYEE");query.setCacheable(true);List users = query.list();SessionFactory.closeSession();Hibernate支持缓存区域的概念。缓存区是给定的一个缓存名称,Hibernate可以通过该名称查找相应缓存。
Session session = SessionFactory.openSession();Query query = session.createQuery("FROM EMPLOYEE");query.setCacheable(true);query.setCacheRegion("employee");List users = query.list();SessionFactory.closeSession();
0 0
- hibernate缓存
- Hibernate 缓存
- Hibernate 缓存
- hibernate 缓存
- hibernate 缓存
- Hibernate缓存
- hibernate缓存
- Hibernate 缓存
- hibernate 缓存
- Hibernate缓存~~~~~~
- Hibernate缓存
- Hibernate缓存
- Hibernate缓存
- Hibernate缓存
- hibernate 缓存
- hibernate缓存
- Hibernate缓存
- hibernate缓存
- YII之片段缓存
- Hinge loss
- 类对象和类的对象的区别
- 把时间当作朋友(写给自己的摘抄小记)
- TabLayout+ViewPage导航栏
- hibernate缓存
- Xcode添加全局断点的方法
- Java多用户商城系统B2B2C源码
- 数据结构实验之二叉树的建立与遍历
- 如何用maven创建spring mvc项目
- ajax调用后台实现文件下载,不弹出下载提示框,无法下载
- log4j和log4j2
- 【机器学习基础】VC维与模型复杂度、样本复杂度
- .Net 中的反射(反射特性)