JPA (5) uses second-level cache, and jpa uses second-level cache

Source: Internet
Author: User

JPA (5) uses second-level cache, and jpa uses second-level cache

Jpa caches are classified into level-1 caches and level-2 caches. level-1 caches are session-level, while level-2 caches are cross-session-level.

The second-level cache is used and Ehcache is used. The first step is to configure the second-level cache in the configuration file.

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

Note:It is a second-level cache. Of course, you need to know what to cache and what jpa products to use (which I understand), so this configuration must be at the end,

<? 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-1" transaction-type = "RESOURCE_LOCAL"> <! -- Configure what ORM product to use --> <! -- Note: 1. the implemented ORM policy inherits javax. persistence. spi. persistenceProvide interface. 2. If there is only one JPA implementation product in the project, it is acceptable if it is not specified --> <provider> org. hibernate. ejb. hibernatePersistence </provider> <! -- Add a persistence class --> <class> com. hotusm. commom. entity. user </class> <class> com. hotusm. commom. entity. phone </class> <class> com. hotusm. commom. entity. firstLover </class> <class> com. hotusm. commom. entity. teacher </class> <class> com. hotusm. commom. entity. student </class> <class> com. hotusm. commom. entity. food </class> <! -- Add second-level cache policy ALL: So ALL object classes are cached NONE: So ALL objects are not cached ENABLE_SELECTIVE: the entity marked with @ Cacheable (true) will be cached DISABLE_SELECTIVE: in addition to the entity UNSPECIFIFD labeled @ Cacheable (true), the cache defaults. The default value of JPA is used, this depends on the specific implementation product of 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 =" 1234 "/> <! -- Configure the jpa implementation product --> <property name = "hibernate. show_ SQL "value =" true "/> <property name =" hibernate. hbm2ddl. auto "value =" update "/> <property name =" hibernate. format_ SQL "value =" true "/> <! -- Use level 2 Cache --> <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>

In this way, you can configure @ Cacheable on the object class to specify whether to use the second-level cache,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.