Hibernate is a widely used ORM (Object-Relational Mapping) tool. Below is an important list of Hibernate interview questions and answers updated for the latest versions.
1) What is Hibernate?
Hibernate is an open-source, lightweight ORM framework for Java that simplifies the storage, retrieval, and manipulation of data in a relational database using object-oriented concepts.
2) Explain Hibernate architecture?
Hibernate architecture consists of core components like:
Configuration: Loads configuration and mappings.
ServiceRegistry: Holds services Hibernate needs at runtime.
SessionFactory: Thread-safe factory for Sessions, created once per application.
Session: Interface for CRUD operations and query execution, non-thread-safe.
Transaction: API for transaction management.
Query/Criteria: For querying the database.
3) What is ORM?
ORM stands for Object-Relational Mapping — a programming approach to map objects in code to relational database tables, allowing developers to work with objects instead of SQL.
4) What are the core interfaces/classes of Hibernate?
Configuration (class)
ServiceRegistry (class)
SessionFactory (interface)
Session (interface)
Transaction (interface)
Query (interface)
Criteria (deprecated since Hibernate 5; replaced by JPA Criteria API)
5) What is SessionFactory?
SessionFactory is a thread-safe factory for Session instances. It holds cached data such as second-level cache (if enabled) and maintains database connection settings.
6) Is SessionFactory thread-safe?
Yes, SessionFactory is designed to be thread-safe and should be instantiated once and reused across threads.
7) What is Session?
Session represents a single unit of work with the database. It is not thread-safe and provides APIs like persist(), update(), delete(), load(), get() for managing entities and queries.
8) Is Session thread-safe?
No, a Session instance is not thread-safe and should not be shared across multiple threads.
9) Difference between session.save() and session.persist()?
Aspect save() persist()
Return value Returns the generated identifier (Serializable) Returns void (no identifier returned)
Behavior Always inserts a new record immediately Follows JPA spec; may delay insert until flush
Cascade and semantics More Hibernate-specific JPA compliant
10) Difference between get() and load()?
Aspect get() load()
Return if not found Returns null Throws ObjectNotFoundException
Database hit Always hits the database Returns proxy without hitting DB initially
Return type Returns actual object Returns proxy object
Use case When you’re unsure if entity exists When sure entity exists; lazy loading
11) Difference between update() and merge()?
Aspect update() merge()
Purpose Reattaches a detached instance to current session Copies the state of a detached object to persistent one
Use case Session must not contain the same entity Works regardless of session state
Throws exception Throws exception if entity exists in session No exception; returns managed entity
Example:
// Detached instance e1
e1.setSalary(70000);
// Trying update on new session throws exception
session2.update(e1); // Error if session2 contains instance with same ID
// Merge copies changes safely
Employee e2 = session2.merge(e1);
12) What are the object states in Hibernate?
Transient: New object, not associated with session, no database row yet.
Persistent: Associated with session, changes are tracked and synchronized with DB.
Detached: Previously persistent but session closed or object evicted; changes not tracked.
13) What are Hibernate’s inheritance mapping strategies?
Single Table (Table per hierarchy): All classes in one table with discriminator column.
Table per Class (Concrete class): Each class mapped to its own table.
Joined (Table per subclass): Parent and child classes mapped to separate tables joined by keys.
14) How to make a class immutable in Hibernate?
Set @Immutable annotation on entity or specify mutable="false" in XML mapping. This means Hibernate will not update the entity once saved.
15) What is automatic dirty checking in Hibernate?
Hibernate automatically detects changes made to persistent objects during a transaction and synchronizes those changes with the database on flush or commit.
16) What types of association mapping does Hibernate support?
One-to-One
One-to-Many
Many-to-One
Many-to-Many
17) Can you perform collection mapping with One-to-One and Many-to-One?
No, collections can only be mapped with One-to-Many and Many-to-Many relationships.
18) What is lazy loading in Hibernate?
Lazy loading delays the fetching of associated objects until they are accessed, improving performance by avoiding unnecessary data retrieval. By default, lazy loading is enabled on associations.
19) What is HQL (Hibernate Query Language)?
HQL is an object-oriented query language similar to SQL but operates on Hibernate entities and their properties instead of database tables.
Advantages:
Database-independent
Uses entity names and properties instead of table/column names
Easier to write and maintain than native SQL
20) Difference between first-level cache and second-level cache?
Aspect First Level Cache Second Level Cache
Scope Session SessionFactory (shared across sessions)
Enabled by default Yes No, must be explicitly enabled
Lifetime Exists during a session’s lifetime Exists beyond session lifetime
Comments
Post a Comment