I enabled hibernate 2nd level caching in my sprint boot 2.1.8 application by adding the following configuration:
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
I also added eh cache dependancy as follows:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.3.11.Final</version>
</dependency>
I annotated an entity to be cacheable:
@Entity(name = "entity")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "entity")
@AttributeOverride(name = "id", column = @Column(name = "entity_id"))
public class Entity extends ItemBase {
private String n1;
private String n2;
private String n3;
private int n4;
@Column(name = "n_5")
private int n5;
//getters and setters
}
Each time i call the repository class (Which extends JpaCrudRepository) and call findAll(); or findOneById(), i see hibernate log HQL queries and database is actually called.
The desired behavior is to fetch the list of entities once from database and keep them in cache, where they should be updated there using read write strategy.
Any idea how can this be acheived?