my-spring-boot-experiments
  • README
  • Spring Batch implementation
  • API Example for Archunit
  • spring-boot-chaos-monkey
  • Grafana LGTM Implementation
  • MongoDb for insertion and search using elastic search
  • OpenSearch Implementation
  • spring-boot-rabbitmq-thymeleaf
  • boot-rest-docs-sample
  • boot-strategy-plugin
  • spring-boot-togglz-sample
  • spring-boot-ultimate-redis
  • GraphQL
    • spring-boot-graphql-querydsl
    • GraphQl implementation using webflux
    • GraphQl implementation using webmvc
  • httpClients
    • http-proxy
    • rest-template
    • rest-client
    • web-client-mvc
    • web-client-webflux
  • jmh-benchmark
  • jpa
    • Custom Sequence
    • Hibernate Envers Implementation
    • multiple datasources using Spring Boot
    • spring-boot-hibernate2ndlevelcache-sample
    • JNDI in embedded Tomcat
    • JPA Jooq Marriage
    • JPA locks implementation
    • read-replica-with-spring-boot
    • KeySet Pagination Using Blaze
    • KeySet Pagination Using Data-JPA
    • MultiTenancy using Hibernate in Spring Data JPA
      • multitenancy-db
      • multidatasource-multitenancy
      • Partitioned (Discriminator) Data – the data for each tenant is partitioned by a discriminator value
      • schema
  • open-api-spring-boot
  • r2dbc
    • r2dbc-jooq
    • PostgreSQL JSON and enum column support
    • PostgreSQL Notify and Listen support using reactive spring boot
    • r2dbc-boot
    • reactive-cache
  • scheduler
    • Scheduling using JobRunr
    • Scheduling using Quartz
    • Scheduling using Shedlock distribution
  • Code Of Conduct
Powered by GitBook
On this page
  • Table of Contents
  • Run tests
  • Run locally
  • Using Testcontainers at Development Time
  • Useful Links
  • Notes
  • Caching Collections (One-Many & Many-Many Relations)
  • Example Configuration

Was this helpful?

Edit on GitHub
  1. jpa

spring-boot-hibernate2ndlevelcache-sample

Previousmultiple datasources using Spring BootNextJNDI in embedded Tomcat

Last updated 4 months ago

Was this helpful?

  • Reduces database round-trips by caching frequently accessed data

  • Provides quick access to cached entities and associations

  • Operates at the SessionFactory level, shared across all sessions

  • Configurable with various cache providers (Redis, Ehcache, Infinispan)

  • Complements the session-scoped first level cache

Leverages Redis to cache frequently accessed entities and associations for faster read performance, reducing round-trips to the database.

Compatibility Note: This implementation has been tested with Redis version 7.4.1 and above.


Table of Contents


Run tests

./mvnw clean verify

Run locally

docker-compose -f docker/docker-compose.yml up -d
./mvnw spring-boot:run -Dspring-boot.run.profiles=local

Using Testcontainers at Development Time

You can run TestApplication.java from your IDE directly. You can also run the application using Maven as follows:

./mvnw spring-boot:test-run

Useful Links

  • Swagger UI: http://localhost:8080/swagger-ui.html

  • Actuator Endpoint: http://localhost:8080/actuator


Notes

  • We need to explicitly set the querycacheHint to customerqueries for enabling 2nd level cache

  • This is enabled only for SessionFactory(i.e as soon as application is closed it will be deleted)

Caching Collections (One-Many & Many-Many Relations)

Collection caching allows you to cache entire collections of associated entities. These collections can be part of your domain model, such as one-to-many or many-to-many relationships between entities.

Collection caching is valuable when dealing with associations between entities that are frequently loaded and where caching can lead to significant performance gains. When you enable collection caching, Hibernate caches entire collections, such as lists or sets, associated with an entity.

When Hibernate caches a collection, it doesn’t cache the entire collection of entities but rather caches the IDs of the entities contained in the collection.

  • Caching only the IDs reduces memory usage compared to caching the entire collection of entities.

  • When a collection is updated, only the relevant IDs need to be invalidated in the cache, rather than the entire collection. This minimizes cache invalidation overhead.

Example Configuration

@Entity
public class Department {
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @OneToMany(mappedBy = "department")
    private List<Employee> employees;
}
Hibernate 2nd Level Cache with Redis
Run tests
Run locally
Using Testcontainers at Development Time
Useful Links
Notes
Caching Collections (One-Many & Many-Many Relations)
Example Configuration