Hibernate Framework Hibernate Caching 1 — Questions and Answers
Question 1: What is the first-level cache in Hibernate?
- The Session cache that stores entities within a single session (Correct answer)
- A database-level cache
- A distributed cache shared across nodes
- A query result cache
Correct answer: The Session cache that stores entities within a single session
The first-level cache is the Session's built-in identity map that caches entities for the duration of a single Session.
Question 2: Can the Hibernate first-level cache be disabled?
- No, it is always enabled and cannot be turned off (Correct answer)
- Yes, by setting hibernate.cache.l1.enabled=false
- Yes, by using StatelessSession
- Yes, by setting hibernate.session.cache=none
Correct answer: No, it is always enabled and cannot be turned off
The first-level cache is mandatory in Hibernate and cannot be disabled; it is always active within a Session.
Question 3: What is the second-level cache in Hibernate?
- A SessionFactory-scoped cache shared across sessions (Correct answer)
- A per-request cache
- A database buffer cache
- A CPU-level cache
Correct answer: A SessionFactory-scoped cache shared across sessions
The second-level cache operates at the SessionFactory level and shares cached data across all Sessions within the application.
Question 4: Which annotation enables second-level caching on a Hibernate entity?
- @Cache (Correct answer)
- @Cacheable
- @L2Cache
- @SecondLevelCache
Correct answer: @Cache
The @Cache annotation (from org.hibernate.annotations) configures second-level cache settings including the CacheConcurrencyStrategy for an entity.
Question 5: Which second-level cache provider is commonly used with Hibernate?
- Ehcache (Correct answer)
- Redis
- Memcached
- Hazelcast
Correct answer: Ehcache
Ehcache is the most commonly used and well-integrated second-level cache provider for Hibernate in enterprise Java applications.
Question 6: What is the query cache in Hibernate?
- A cache that stores the results of HQL/Criteria queries (Correct answer)
- A cache for SQL execution plans
- A cache for entity metadata
- A cache for JDBC connections
Correct answer: A cache that stores the results of HQL/Criteria queries
The query cache stores the result sets of Hibernate queries so that repeated identical queries can be served from cache without hitting the database.
What is the first-level cache in Hibernate?