Intermediate and advanced Hibernate questions on ORM basics, caching, transactions, fetching, entity states and performance, with short answers.
Related: Java interview questions and contract Hibernate developers.
Questions and answers
What is Hibernate and how does it work as an ORM tool?
Hibernate is a Java object-relational mapping (ORM) framework. It maps Java classes to database tables, so developers read and write data through Java objects instead of writing SQL for every operation.
Explain first-level and second-level caching in Hibernate.
The first-level cache belongs to the session and is always on. The second-level cache belongs to the SessionFactory, is shared across sessions, and has to be configured explicitly with a cache provider such as EHCache or Infinispan.
How does Hibernate manage transactions?
Through the Transaction API on top of database transactions. You begin a transaction explicitly, then commit it or roll it back, which keeps data consistent when something fails halfway.
What is the difference between eager and lazy fetching?
Eager fetching loads associated entities together with the main entity. Lazy fetching waits until the association is first accessed. Lazy is the better default for performance, unless the association is needed every time.
How do you improve Hibernate performance?
Use batching, caching, the right fetching strategy, proper indexes and tuned queries. Above all, avoid the N+1 select problem, where loading a list triggers one extra query per row.
What is the difference between get() and load()?
get() hits the database straight away and returns null if the row does not exist. load() returns a proxy and only throws ObjectNotFoundException when you access it and the row turns out not to exist.
What are the states of an entity in Hibernate?
Transient: new and not attached to a session. Persistent: managed by an open session. Detached: was persistent, but its session has closed. Removed: scheduled for deletion.
What is the purpose of the @Version annotation?
@Version turns on optimistic locking. Hibernate checks the version field on every update, so two users cannot silently overwrite each other's changes.
What is the difference between save() and persist()?
save() is Hibernate-specific, returns the generated identifier and can be called outside a transaction. persist() comes from the JPA specification, returns void and must run inside a transaction.
What is the query cache in Hibernate?
The query cache stores the results of a query as a list of identifiers, not the entities themselves. It only works when the second-level cache is enabled and the query is marked as cacheable.
Keep reading
Same skill, other angles
- Hire contract Hibernate developers for production Java data layersHire developers
- Hire contract Java developers for Spring Boot and microservicesHire developers
- Java developer salary in IndiaSalaries & rates
- Senior Java backend developer (remote)Contract jobs
- How a fintech firm hired 15 Java contractors in 48 hoursCase studies

