默认情况下开启一级缓存,如下
@Test public void testSecondLevelCache() { User user = entityManager.find(User.class, 1); System.out.println("------------------");//只发送一条SQL查询语句 User user1 = entityManager.find(User.class, 1); }
@Test public void testSecondLevelCache() { User user = entityManager.find(User.class, 1); entityTransaction.commit(); entityManager.close(); System.out.println("------------------"); entityManager = entityManagerFactory.createEntityManager(); entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); User user1 = entityManager.find(User.class, 1); //发送两条查询语句 }
下面配置二级缓存
persistance.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="Jpa" transaction-type="RESOURCE_LOCAL"> <!-- 配置使用什么ORM产品来作为JPA的实现 1.实际上配置的是javx.persistance.spi.PersistanceProvider接口的实现类 2.若JPA项目中只有一个JPA的实现产品,也可以不配置该节点 --> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- 添加持久化类 --> <class>com.jpa.beans.User</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:///test" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="" /> <!-- 配置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>
加入ehcache相关jar包
ehcache-core-2.4.3.jar
hibernate-ehcache-4.3.5.Final.jar
slf4j-api-1.6.1.jar
加入ehcache.xml(hibernate-release-4.3.5.Final\project\etc目录下)
在实体类上加入Cacheable注解
@Cacheable(true) @Table(name = "jps_user") @Entity public class User { @GeneratedValue @Id private Integer id; private String username; private String password; //getter setter }
测试
@Test public void testSecondLevelCache() { User user = entityManager.find(User.class, 1); entityTransaction.commit(); entityManager.close(); System.out.println("------------------"); entityManager = entityManagerFactory.createEntityManager(); entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); User user1 = entityManager.find(User.class, 1); }
以上测试代码只会发送一条SQL查询语句。二级缓存启用成功。