Hibernate 系列教程16-二级缓存
pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
Product
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:多方:多个产品属于同一个产品类型
*/
public class Product {
private Long id;
private String name;
private ProductType type;// 多对一
private BigDecimal price;
ProductType
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:双向一方:一个产品类型下面有n个产品
*/
public class ProductType {
private Long id;
private String name;
private Set<Product> products = new HashSet<Product>();// 一对多:集合Set
Product.hbm.xml
<class name="Product">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<property name="price" />
<many-to-one name="type" class="ProductType" column="type_id" />
</class>
ProductType.hbm.xml
<class name="ProductType">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<set name="products" inverse="true">
<!-- 配置多方Product的外键名称 -->
<key column="type_id" />
<one-to-many class="Product" />
</set>
</class>
hibernate.cfg.xml
<!-- 开启二级缓存 -->
<property name="cache.use_second_level_cache">true</property>
<!-- 二级缓存的实现 -->
<property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 开启查询缓存 -->
<property name="cache.use_query_cache">true</property>
<mapping resource="com/jege/hibernate/second_level_cache/ProductType.hbm.xml" />
<mapping resource="com/jege/hibernate/second_level_cache/Product.hbm.xml" />
<!-- 类的二级缓存 -->
<class-cache class="com.jege.hibernate.second_level_cache.Product" usage="read-write" />
<class-cache class="com.jege.hibernate.second_level_cache.ProductType" usage="read-write" />
<!-- 类的集合二级缓存 -->
<collection-cache collection="com.jege.hibernate.second_level_cache.ProductType.products" usage="read-write" />
MainTest
public class MainTest {
@Before
public void save() throws Exception {
ProductType type = new ProductType();
type.setName("类型1");
Product product1 = new Product("产品1");
product1.setPrice(new BigDecimal(200));
Product product2 = new Product("产品2");
product2.setPrice(new BigDecimal(300));
// 建立多方到一方的关系
product1.setType(type);
product2.setType(type);
// 建立一方多到方的关系(出现多方在一方的索引)
type.getProducts().add(product2);
type.getProducts().add(product1);
Session session = HibernateUtils.INSTANCE.getSession();
session.beginTransaction();
session.save(type);// 持久化状态
session.save(product1);// 持久化状态
session.save(product2);// 持久化状态
session.getTransaction().commit();
session.close();
}
// 模拟同一个SessionFactory,不同session获取同一个OID对象
// 只发出一条sql:二级缓存命中
@Test
public void get() throws Exception {
Session session1 = HibernateUtils.INSTANCE.getSession();
Product product1 = (Product) session1.get(Product.class, 1L);
Product product2 = (Product) session1.get(Product.class, 1L);// 一级缓存命中
session1.close();
Session session2 = HibernateUtils.INSTANCE.getSession();
Product product3 = (Product) session2.get(Product.class, 1L);// 二级缓存命中
Product product4 = (Product) session2.get(Product.class, 1L);// 一级缓存命中
session2.close();
}
// 模拟同一个SessionFactory,不同session获取同一个OID对象里面的集合
// 只发出一条sql:二级缓存命中
@Test
public void get2() throws Exception {
Session session1 = HibernateUtils.INSTANCE.getSession();
ProductType productType1 = (ProductType) session1.get(ProductType.class, 1L);
System.out.println(productType1.getProducts().size());
session1.close();
Session session2 = HibernateUtils.INSTANCE.getSession();
ProductType productType2 = (ProductType) session2.get(ProductType.class, 1L);
System.out.println(productType2.getProducts().size());
session2.close();
}
}
源码地址
https://github.com/je-ge/hibernate
如果觉得我的文章对您有帮助,请予以打赏。您的支持将鼓励我继续创作!谢谢!
Hibernate 系列教程16-二级缓存的更多相关文章
- Hibernate 系列教程17-查询缓存
在二级缓存配置成功的基础上进行查询缓存配置 Product public class Product { private Long id; private String name; Product.h ...
- 【Hibernate】Hibernate系列7之二级缓存
二级缓存 7.1.概述 7.2.配置方法
- Hibernate 系列教程15-一级缓存
Product public class Product { private Long id; private String name; Product.hbm.xml <class name= ...
- CRL快速开发框架系列教程六(分布式缓存解决方案)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- CRL快速开发框架系列教程五(使用缓存)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- Hibernate的查询,二级缓存,连接池
Hibernate的查询,二级缓存,连接池 1.Hibernate查询数据 Hibernate中的查询方法有5中: 1.1.Get/Load主键查询 使用get或者load方法来查询,两者之间的区别在 ...
- hibernate框架学习之二级缓存
缓存的意义 l应用程序中使用的数据均保存在永久性存储介质之上,当应用程序需要使用数据时,从永久介质上进行获取.缓存是介于应用程序与永久性存储介质之间的一块数据存储区域.利用缓存,应用程序可以将使用的数 ...
- NHibernate教程(21)——二级缓存(下)
本节内容 引入 使用NHibernate二级缓存 启用缓存查询 管理NHibernate二级缓存 结语 引入 这篇我还继续上一篇的话题聊聊NHibernate二级缓存剩下的内容,比如你修改.删除数据时 ...
- NHibernate教程(20)——二级缓存(上)
本节内容 引入 介绍NHibernate二级缓存 NHibernate二级缓存提供程序 实现NHibernate二级缓存 结语 引入 上一篇我介绍了NHibernate内置的一级缓存即ISession ...
随机推荐
- MongoDB数据模型(一)
原文地址 一.数据模型介绍 MongoDB中的数据有着灵活的架构.与SQL数据库不同,因为SQL数据库必须先定义表结构,然后才能向其中插入数据,而MongoDB的集合不强制任何文档结构.这个灵活性方便 ...
- 触动精灵远程Log模块
一.功能 lua log方法能够自动发现同一网段下面的log服务器 lua log方法能够主动将log发给服务器 lua 客户端进程重启服务端不存在影响 二.实现 服务器使用python编写: 启动一 ...
- jQuery 学习小结
1,jQuery是一个简单的JavaScript库,提供了一系列辅助函数:以下简称jq; 使用jq时,通常将jq代码放到head部分的事件处理方法中,也可以将其单独写出 .js 文件,引入:但无论哪种 ...
- webapi中Route标签定义可选参数
Optional URI Parameters and Default Values You can make a URI parameter optional by adding a questio ...
- Leetcode016 3Sum Closest
public class S016 { //借鉴S015的思想,只是稍微有点慢 public int threeSumClosest(int[] nums, int target) { Arrays. ...
- 极光推送 PHP sdk
<?php defined('IN_WZ') or exit('No direct script access allowed'); /** * Created by PhpStorm. * U ...
- 解决time_wait过多的问题
#netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’ LAST_ACK 14SYN_RECV 348ESTABLI ...
- FbinstTool(U盘启动盘制作工具) v1.606 免费绿色版
软件名称: FbinstTool(U盘启动盘制作工具) v1.606 免费绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP / W ...
- Java--重载与重写
父类(Parent): public class Parent { public String name = "parent 父类属性"; public void say(){ S ...
- subnetting and the subnet mask
原文:https://www.techopedia.com/6/28587/internet/8-steps-to-understanding-ip-subnetting/5 Step 4 - Sub ...