Prev Next

Hibernate / Hibernate 7 Basics Interview Questions

What is the second-level cache in Hibernate 7 and how does it work with StatelessSession?

The second-level cache is an optional cross-session application-wide cache. Hibernate 7 changes StatelessSession's behaviour: it now reads/writes the L2 cache by default (was bypassed in v6).

Cache level comparison
AspectFirst-level cacheSecond-level cache
ScopePer SessionPer SessionFactory (application lifetime)
MandatoryYesOptional
Shared across sessionsNoYes
ProviderBuilt-inInfinispan, Ehcache 3, Caffeine
// Enable L2 cache on an entity:
@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Country {
    @Id private Long id;
    private String name;
}

// Spring Boot 4 configuration:
// spring.jpa.properties.hibernate.cache.use_second_level_cache=true

// HIBERNATE 7: StatelessSession now uses L2 cache by default!
// To bypass (for bulk processing):
StatelessSession ss = sf.openStatelessSession();
ss.setCacheMode(CacheMode.IGNORE);  // bypass L2 cache

What is the key Hibernate 7 change in how StatelessSession interacts with the second-level cache?
What ConcurrencyStrategy would you use for entity read frequently and rarely updated?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!
Acorns Logo

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Robinhood Logo

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull Logo

Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Hibernate 7 and when was it released? What is ORM and why is Hibernate used instead of writing raw JDBC? How do you configure Hibernate 7 with Maven? What is an entity in Hibernate 7 and how do you define one? What are entity states in Hibernate 7 and how do they transition? What is the biggest breaking change in Hibernate 7: detached entity reassociation removal? What is the Session in Hibernate 7 and what are its core CRUD operations? What is the Hibernate SessionFactory and how do you create one? What is HQL (Hibernate Query Language) in Hibernate 7? What is JPQL vs HQL in Hibernate 7? How does Hibernate 7 handle primary key generation strategies? What is the first-level cache (persistence context) in Hibernate 7? What is the second-level cache in Hibernate 7 and how does it work with StatelessSession? What are lazy and eager fetching in Hibernate 7 and how do you avoid the N+1 problem? How do you map one-to-many and many-to-one relationships in Hibernate 7? What is the StatelessSession in Hibernate 7 and when should you use it? What is the Criteria API in Hibernate 7 and what are SelectionSpecification and RestrictionSpecification? How does @Embeddable and @Embedded mapping work in Hibernate 7? What are the new FindOptions in Jakarta Persistence 3.2 and Hibernate 7? What is optimistic locking in Hibernate 7 and how do you implement it? What is pessimistic locking in Hibernate 7 and what are the improvements? What is the @ManyToMany relationship in Hibernate 7 and how do you map it? What is Hibernate's flush behaviour and how does it affect SQL execution? What are named queries in Hibernate 7 and how are they type-safe with TypedQueryReference? What is the @Inheritance mapping in Hibernate 7 and what strategies are available? What is the @Filter and auto-enabled filters feature in Hibernate 7? What is key-based pagination in Hibernate 7 and how does it differ from offset pagination? What is Jakarta Data 1.0 and how does it integrate with Hibernate 7? What is the @NaturalId feature in Hibernate 7 and what is @NaturalIdClass? How does Hibernate 7 support vector data types for AI/ML applications? What are the key removed APIs in Hibernate 7 and how do you migrate? What is @SQLRestriction and @SQLJoinTableRestriction in Hibernate 7? How does Hibernate 7 handle schema validation and generation (hbm2ddl)? What are Hibernate 7's improvements to multi-tenancy support? What is the @Check constraint and @ColumnDefault annotation in Hibernate 7? What is @BatchSize and how does it solve the N+1 problem differently from JOIN FETCH? What is Hibernate 7's @Struct mapping for database composite types? What are Hibernate 7's logging improvements for SQL and parameter binding? How does Hibernate 7 work with Spring Boot 4 - what is auto-configured? What are the key Hibernate 7 vs Hibernate 6 differences interviewers ask about?
Show more question and Answers...

Maven

Comments & Discussions