一级缓存

jpa默认是有一级缓存的。

  1. @Test
  2. public void testCache() {
  3. Customer customer = entityManager.find(Customer.class, 3);
  4. System.out.println(customer.getLastName());
  5. System.out.println("-------------");
  6. Customer customer1 = entityManager.find(Customer.class, 3);
  7. System.out.println(customer1.getLastName());
  8. }

以上代码, 只发送一次sql语句

二级缓存

  1. @Test
  2. public void testCache1() {
  3. Customer customer = entityManager.find(Customer.class, 3); //这里一级缓存为什么没有生效呢????
  4. System.out.println(customer.getLastName());
  5. transaction.commit();
  6. entityManager.close();
  7. System.out.println("--------------");
  8. entityManager = entityManagerFactory.createEntityManager();
  9. transaction = entityManager.getTransaction();
  10. transaction.begin();
  11. Customer customer1 = entityManager.find(Customer.class, 3);
  12. System.out.println(customer1.getLastName());
  13. }

以上代码, 会发送二次sql语句,开启二级缓存,需要进行配置:
1.修改persistence.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence version="2.0"
  3. xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  5. <persistence-unit name="jpa-1" transaction-type="RESOURCE_LOCAL">
  6. <!--
  7. 配置使用什么 ORM 产品来作为 JPA 的实现
  8. 1. 实际上配置的是 javax.persistence.spi.PersistenceProvider 接口的实现类
  9. 2. JPA 项目中只有一个 JPA 的实现产品, 则也可以不配置该节点.
  10. -->
  11. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  12. <!-- 添加持久化类 -->
  13. <class>cn.carven.jpa.helloworld.Customer</class>
  14. <class>cn.carven.jpa.helloworld.Order</class>
  15. <class>cn.carven.jpa.helloworld.Department</class>
  16. <class>cn.carven.jpa.helloworld.Manager</class>
  17. <class>cn.carven.jpa.helloworld.Item</class>
  18. <class>cn.carven.jpa.helloworld.Category</class>
  19. <!--
  20. 配置二级缓存的策略
  21. ALL:所有的实体类都被缓存
  22. NONE:所有的实体类都不被缓存.
  23. ENABLE_SELECTIVE:标识 @Cacheable(true) 注解的实体类将被缓存
  24. DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的所有实体类
  25. UNSPECIFIED:默认值,JPA 产品默认值将被使用
  26. -->
  27. <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
  28. <properties>
  29. <!-- 连接数据库的基本信息 -->
  30. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  31. <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"/>
  32. <property name="javax.persistence.jdbc.user" value="root"/>
  33. <property name="javax.persistence.jdbc.password" value="123456"/>
  34. <!-- 配置 JPA 实现产品的基本属性. 配置 hibernate 的基本属性 -->
  35. <property name="hibernate.format_sql" value="true"/>
  36. <property name="hibernate.show_sql" value="true"/>
  37. <property name="hibernate.hbm2ddl.auto" value="update"/>
  38. <!-- 二级缓存相关 -->
  39. <property name="hibernate.cache.use_second_level_cache" value="true"/>
  40. <property name="hibernate.cache.region.factory_class"
  41. value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
  42. <property name="hibernate.cache.use_query_cache" value="true"/>
  43. </properties>
  44. </persistence-unit>
  45. </persistence>

2.在实体类中添加@Cacheable(true)注解