I Use Spring Boot configuration for Second level Cache for my Master tables. I'm not getting the second level cache configured in Spring Boot. Need to get how the Caching is done and also the retrieval.
This is my Application.java class (SpringBootServletinitializer).. And I added a test method allCodeValues() just to verify whether I get a hibernate query fired only once. But I see the query is fired twice and thus says the repository call is not cached in hibernate. Please help me in the exact configuration of SpringBoot for EhCaching...
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception{
SpringApplication.run(Application.class, args);
}
@Autowired
WorkflowListRepository wflListRepo;
@Bean(name="getAllCodeValue")
public List<WorkFlowList> allcodeValues(){
List<WorkFlowList> wflList= wflListRepo.getAllDocuments();
wflList= wflListRepo.getAllDocuments();
return wflList;
}
}
Below is the configuration for JPA Persistence
@Bean(name = "entityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdaptor());
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
entityManagerFactoryBean.setJpaProperties(jpaHibernateProperties());
return entityManagerFactoryBean;
}
private Properties jpaHibernateProperties() {
Properties properties = new Properties();
properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT,env.getProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
properties.put(PROPERTY_NAME_HIBERNATE_HBM_AUTO,env.getProperty(PROPERTY_NAME_HIBERNATE_HBM_AUTO));
properties.put("spring.jpa.properties.hibernate.cache.use_second_level_cache", "true");
properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
properties.put("hibernate.cache.default_cache_concurrency_strategy", "NONSTRICT_READ_WRITE");
properties.put("hibernate.cache.region_prefix", "valCache");
properties.put("javax.persistence.sharedCache.mode", "ALL");
}
I use JPARepository to communicate with the Entities... Here I use WorklfowRepository to get the Data in Second level Cache..
@Repository
public interface WorkflowListRepository extends JpaRepository<WorkFlowList, String>{
public List<WorkFlowList> getAllDocuments();
}
In Entity I added the annotations @Cacheable and @Cache like below
@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
@Table(name = "WORKFLOW_LIST")
@Access(AccessType.FIELD)
@NamedQuery(name="WorkFlowList.getAllDocuments",query = "from WorkFlowList work")
public class WorkFlowList implements in.vit.leasing.generic.entity.Entity<WorkFlowList>{
.......
}