To enable Hibernate Level 2 caching:
- Include
hibernate-ehcache
in your project - Create an
ehcache.xml
file insrc/main/resources
that defines each type of cache that you want; you can define both entity and relationship caches - Update
spring.jpa.properties.hibernate
inapplication.yml
to enable second level caching with ehcache - Annotate each cacheable entity bean with
@Cacheable
and@org.hibernate.annotations.Cache
as shown in theCourse
entity bean - Annotate each cacheable relationship with
@org.hibernate.annotations.Cache
again as shown inCourse::students
- Queries are cached by annotating finder methods in repository beans with
@QueryHints
as shown inTeacherRepository
- Verify via a test case that the configuration works