框架学习之JPA(五)

JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

Sun引入新的JPA ORM规范出于两个原因:其一,简化现有Java EE和Java SE应用开发工作;其二,Sun希望整合ORM技术,实现天下归一。

学习视频:尚硅谷框架jpa学习(有兴趣的同学留言邮箱)

使用软件:eclipse

Java版本:jdk8

本节目录

五、JPA_二级缓存

1.添加jar包

2.修改persistence.xml配置文件

3添加ehcache.xml配置文件

4.启动ehcache.xml配置文件,在persistence.xml配置文件中配置

5.需要缓存的类上添加注解

五、JPA_二级缓存

一级缓存:同一个entityManager中不用重复在数据库中查询同一个数据

二级缓存:

  • <shared-cache-mode> 节点:若 JPA 实现支持二级缓存,该节点可以配置在当前的持久化单元中是否启用二级缓存,可配置如下值:

    • ALL:所有的实体类都被缓存
    • NONE:所有的实体类都不被缓存.
    • ENABLE_SELECTIVE:标识 @Cacheable(true) 注解的实体类将被缓存
    • DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的所有实体类
    • UNSPECIFIED:默认值,JPA 产品默认值将被使用

1.添加jar包

2.修改persistence.xml配置文件

  • 查看第四步,防止一的persistence.xml文件

3.添加ehcache.xml配置文件

<ehcache>
<!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following properties are translated: user.home - User's home directory user.dir - User's current working directory java.io.tmpdir - Default temp file path --> <diskStore path="java.io.tmpdir"/> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <!--Predefined caches. Add your cache configuration settings here. If you do not have a configuration for your cache a WARNING will be issued when the CacheManager starts The following attributes are required for defaultCache: name - Sets the name of the cache. This is used to identify the cache. It must be unique. maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <!-- Sample cache named sampleCache1 This cache contains a maximum in memory of 10000 elements, and will expire an element if it is idle for more than 5 minutes and lives for more than 10 minutes. If there are more than 10000 elements it will overflow to the disk cache, which in this configuration will go to wherever java.io.tmp is defined on your system. On a standard Linux system this will be /tmp" --> <cache name="sampleCache1" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <!-- Sample cache named sampleCache2 This cache contains 1000 elements. Elements will always be held in memory. They are not expired. --> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> --> <!-- Place configuration for your caches following --> </ehcache>

  

4.启动ehcache.xml配置文件,在persistence.xml配置文件中配置

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"

xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="SGG-jpa" transaction-type="RESOURCE_LOCAL">

<!-- 配置使用什么 ORM 产品来作为 JPA 的实现 1. 实际上配置的是 javax.persistence.spi.PersistenceProvider

接口的实现类 2. 若 JPA 项目中只有一个 JPA 的实现产品, 则也可以不配置该节点. -->

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<!-- 添加持久化类 -->

<class>hue.edu.xiong.jpa.Customer</class>

<class>hue.edu.xiong.jpa.Order</class>

<class>hue.edu.xiong.jpa.Manager</class>

<class>hue.edu.xiong.jpa.Department</class>

<class>hue.edu.xiong.jpa.Item</class>

<class>hue.edu.xiong.jpa.Category</class>

<!-- 配置二级缓存的策略

ALL:所有的实体类都被缓存

  NONE:所有的实体类都不被缓存.

  ENABLE_SELECTIVE:标识 @Cacheable(true)注解的实体类将被缓存

DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的所有实体类

UNSPECIFIED:默认值,JPA产品默认值将被使用

-->

<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

<properties>

<!-- 连接数据库的基本信息 -->

<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />

<property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa" />

<property name="javax.persistence.jdbc.user" value="root" />

<property name="javax.persistence.jdbc.password" value="admin" />

<!-- 配置JPA实现产品的基本属性,配置hibernate -->

<property name="hibernate.format_sql" value="true" />

<property name="hibernate.show_sql" value="true" />

<property name="hibernate.hbm2ddl.auto" value="update" />

<!-- 二级缓存相关 -->

<property name="hibernate.cache.use_second_level_cache"

value="true" />

<property name="hibernate.cache.region.factory_class"

value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />

<property name="hibernate.cache.use_query_cache" value="true" />

</properties>

</persistence-unit>

</persistence>

 

5.需要缓存的类上添加注解

测试案例:

@Test

public void testSecondLevelCache(){

Customer customer1 = entityManager.find(Customer.class, 1);

transaction.commit();

entityManager.close();

entityManager = entityManagerFactory.createEntityManager();

transaction = entityManager.getTransaction();

transaction.begin();

Customer customer2 = entityManager.find(Customer.class, 1);

}

  

JPA学习(五、JPA_二级缓存)的更多相关文章

  1. Hibernate JPA 中配置Ehcache二级缓存

    在Hibernate3 JPA里配置了一下非分布式环境的二级缓存,效果不错.具体过程如下: 1, 需要引入的jar包 http://ehcache.org/downloads/catalog 下载的包 ...

  2. Hibernate基础学习(六)—Hibernate二级缓存

    一.概述      Session的缓存是一块内存空间,在这个内存空间存放了相互关联的Java对象,这个位于Session缓存内的对象也被称为持久化对象,Session负责根据持久化对象的状态来同步更 ...

  3. mybatis源码学习:一级缓存和二级缓存分析

    目录 零.一级缓存和二级缓存的流程 一级缓存总结 二级缓存总结 一.缓存接口Cache及其实现类 二.cache标签解析源码 三.CacheKey缓存项的key 四.二级缓存TransactionCa ...

  4. Hibernate学习笔记(六) — Hibernate的二级缓存

    我们知道hibernate的一级缓存是将数据缓存到了session中从而降低与数据库的交互.那么二级缓存呢? 一.应用场合 比方.在12306购票时.须要选择出发地与目的地,假设每点一次都与数据库交互 ...

  5. mybatis源码学习(三)-一级缓存二级缓存

    本文主要是个人学习mybatis缓存的学习笔记,主要有以下几个知识点 1.一级缓存配置信息 2.一级缓存源码学习笔记 3.二级缓存配置信息 4.二级缓存源码 5.一级缓存.二级缓存总结 1.一级缓存配 ...

  6. mybatis 学习五 二级缓存不推荐使用

    mybatis 二级缓存不推荐使用 一 mybatis的缓存使用. 大体就是首先根据你的sqlid,参数的信息自己算出一个key值,然后你查询的时候,会先把这个key值去缓存中找看有没有value,如 ...

  7. JPA学习笔记(11)——使用二级缓存

    一级缓存 查询两次id为1的user User user1 = entityManager.find(User.class, 1); User user2 = entityManager.find(U ...

  8. 【Java EE 学习 48】【Hibernate学习第五天】【抓取策略】【二级缓存】【HQL】

    一.抓取策略. 1.hibernate中提供了三种抓取策略. (1)连接抓取(Join Fetch):这种抓取方式是默认的抓取方式.使用这种抓取方式hibernate会在select中内连接的方式获取 ...

  9. Mybatis学习(五)————— 延迟加载和缓存机制(一级二级缓存)

    一.延迟加载 延迟加载就是懒加载,先去查询主表信息,如果用到从表的数据的话,再去查询从表的信息,也就是如果没用到从表的数据的话,就不查询从表的信息.所以这就是突出了懒这个特点.真是懒啊. Mybati ...

随机推荐

  1. 【CUDA开发】CUDA从入门到精通

    CUDA从入门到精通(零):写在前面 在老板的要求下,本博主从2012年上高性能计算课程开始接触CUDA编程,随后将该技术应用到了实际项目中,使处理程序加速超过1K,可见基于图形显示器的并行计算对于追 ...

  2. 【Qt开发】QTableWidget的详细设置

    在使用Qt不多的日子里,已经两次用到了QTableWidget这个控件,也慢慢的习惯和喜欢上了它.再使用QTableWidget的时候,已不像刚开始使用时的迷茫.嗯嗯.现在就来总结总结我与QTable ...

  3. 微信小程序request请求封装,验签

    1/ 公共文件util添加 request请求 //简单封装请求 function request(params, path, isShowLoading = true, goBack = false ...

  4. reactstrap,scrollbar组件

    react-script 编译,部署,sass,less,test,helmet等 https://github.com/facebookincubator/create-react-app/blob ...

  5. javaScript中==和===对数组、对象的判断是它们是否同一个实例对象

      问题描述 在实现业务时,大量用到了 if(a === b)这样的判断,但有一个类似判断一直进不去这个if条件, a === b 返回的一直是false,但是其他几个类似判断,都正常触发条件. 原因 ...

  6. [Python3] 025 包

    目录 1. 模块 1.1 模块是什么? 1.2 为什么用模块? 1.3 如何定义模块? 1.4 如何使用模块? 1.4.1 例子1 1.4.2 例子2 1.4.3 例子3 1.4.4 例子4 1.4. ...

  7. 小记---------FLUM的三种配置方式:spooldir、exec、hadoop sink

    FLUM概述     是一个分布式的数据收集系统,具有高可靠.高可用.事务管理.失败重启等功能,数据处理速度快,完全可以用于生产环境   核心:agent(是FLUM的一个代号,名字    ).age ...

  8. Luogu P4438 [HNOI/AHOI2018]道路

    题目 注意到\(n\)不大并且深度不大. 记\((u,ls_u)\)为\(L\)边,\((u,rs_u)\)为\(r\)边. 所以我们可以设\(f_{p,i,j}\)表示从根到\(p\)有\(i\)条 ...

  9. scrapy之盗墓笔记三级页面爬取

    #今日目标 **scrapy之盗墓笔记三级页面爬取** 今天要爬取的是盗墓笔记小说,由分析该小说的主要内容在三级页面里,故需要我们 一一解析 *代码实现* daomu.py ``` import sc ...

  10. PythonDay10

    第十章函数进阶 今日内容 函数的参数 动态参数 动态接收位置参数 动态接收关键字参数 函数的注释 名称空间 函数的嵌套 global.nonlocal global的宗旨 nonlocal宗旨 1.函 ...