0

I want to implement second level cache for one of entities.But i am fetching data using field which is not ID.I read in many posts that second level cache works only if we are fetching data using id.Is there any way,so that i can implement second level cache to fetch data using field other than id? I am using Eh-cache,JPA repository and spring Boot.How can I implement query cache?

in properties file:

hibernate.cache.use_second_level_cache=true

hibernate.cache.use_query_cache=true

hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

Repository.java

public interface SomeRepository extends JpaRepository<SomeClass, Long> {

   @Query("SELECT .....WHERE id= :someId")
   @QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
   List<Lookup> findAllByProjectId(@Param("someId") Long someId);

}
Selaron
  • 6,105
  • 4
  • 31
  • 39

1 Answers1

0

In general you are asking how to cache query results. Hibernate has a QueryCache for this purpose:

Quotes from documentation:

To use query caching, you will first need to enable it with the following configuration property:

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

Example 457. Caching query using JPA

List<Person> persons = entityManager.createQuery(
  "select p " +
  "from Person p " +
  "where p.name = :name", Person.class)
.setParameter( "name", "John Doe")
.setHint( "org.hibernate.cacheable", "true")
.getResultList();

Example 458. Caching query using Hibernate native API

List<Person> persons = session.createQuery(
  "select p " +
  "from Person p " +
  "where p.name = :name")
.setParameter( "name", "John Doe")
.setCacheable(true)
.list();
Selaron
  • 6,105
  • 4
  • 31
  • 39
  • but its session based cache –  Mar 29 '19 at 11:43
  • how to do this annotation based? –  Mar 29 '19 at 11:53
  • The query cache is managed in a cache region of EHcache for example. What do you mean by "session based"? And how do you expect this to be configurable via annotations? There is no annotation in your question yet. – Selaron Mar 29 '19 at 12:03
  • is query cache shared among sessions? And I am using jpa repositories and spring boot?Do i need to add @cachable on entity and in repository as well? –  Mar 29 '19 at 12:06
  • It should be shared across sessions afaik. As caching must be enabled per query, you can possibly enable the JPA query hint mentioned above for your spring JPA repository via annotation [as described in Spring documentation](https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html#jpa.query-hints). – Selaron Mar 29 '19 at 12:28
  • Did you also read this? [Spring Boot + JPA2 + Hibernate - enable second level cache](https://stackoverflow.com/questions/31585698/spring-boot-jpa2-hibernate-enable-second-level-cache#31590501) And I'm not sure what login/logout means. There are hibernate sessions (or JPA sessions) and there are user web sessions. Both solve very different problems in different contexts. – Selaron Mar 29 '19 at 12:52