JPA Caching Example

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Previously, we have seen the cocnept of JPA Caching. Let us now see a JPA Caching Example which shows how to configure JPA Cache and cache enttities using @Cacheable annotation. We know that EntityManager is first level cache and EntityManagerFactory is second level cache. Let us see how it can be used in JPA Caching example.

 

Configure JPA Caching Example:

You have to use a JPA implementation that supports caching. I’ve used eclipseLink. To configure cache, you have to use shared-cache-mode element in persistence.xml  and @Cacheable annotation. Cache mode settings are as below.

  • ALL : All entities are stored in second level cache for a given persistence unit.
  • NONE : No entities are stored in second level cache.
  • ENABLE_SELECTIVE: Cache only those entities which are explicitly marked with @Cacheable(true) or @Cacheable as default value for @Cacheable is true. 
  • DISABLE_SELECTIVE:  Cache all entities except for those who are marked with @Cacheable(false).
  • UNSPECIFIED: Behaviour is undefined. Persistence provider defaults may apply.

Now that we have seen rules for second level caching, let us see real JPA Caching Example to verify the same.

Consider we have three entities, Animal, Bike and Computer. 

Animal.java

 Bike.java

 Computer.java

Note that Animal is annotated with @Cacheable which is equivalent to @Cacheable(true), Bike is annotated with @Cacheable(false) and Computer  is not annotated with @Cacheable. 

Lets test JPA Caching Example using code below.

Set shared-cache-mode in persistence.xml to ALL  as below.

Run program and output will be.

Animal in Cache: true
Bike in Cache : true
Computer in Cache : true
Animal in Cache after evict: false
Bike in Cache after evict: false
Computer in Cache after evict: false

Note that all entities are cached irrespective of their @Cacheable annotation when shared-cache-mode is set to ALL.

Now set it to ENABLE_SELECTIVE using  <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

Run program and output will be

Animal in Cache: true
Bike in Cache : false
Computer in Cache : false
Animal in Cache after evict: false
Bike in Cache after evict: false
Computer in Cache after evict: false

Note that only Animal was cached as it is the only entity annotated with @Cacheable(true)

Now set it to DISABLE_SELECTIVE using  <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>

Run program and output will be

Animal in Cache: true
Bike in Cache : false
Computer in Cache : true
Animal in Cache after evict: false
Bike in Cache after evict: false
Computer in Cache after evict: false

i.e. All entities except those with @Cacheable(false) are cached.

NOTE: Caching behaviour is not required to be supported by persistence providers. So take care while developing portable applications.

I hope this JPA Caching example helped understand how to configure and use cache.

Share Button

4 comments for “JPA Caching Example

  1. Jonathan
    March 10, 2016 at 11:49 pm

    Excellent article, thanks!

  2. Ahmed El Sabagh
    March 29, 2016 at 3:33 pm

    Thank You For This Simple explanation

  3. George Fulgeanu
    September 1, 2016 at 6:12 pm

    do all persistence providers implement second level caching, it is not necesary in the jpa specifications : https://docs.oracle.com/javaee/7/tutorial/persistence-cache002.htm (first note)

    • September 2, 2016 at 6:03 pm

      Yes, it is not necessary for persistence providers to implement caching functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *