
Here are top Hibernate interview questions,
1. What is Hibernate and why is it
used?
Hibernate
is an open-source, object-relational mapping (ORM) framework that simplifies
database access in Java applications by mapping Java objects to database
tables. It provides a way to store, retrieve, and manage Java objects in a
relational database.
It
simplifies database access by providing a layer of abstraction that hides the
complexity of SQL and JDBC.
2. Explain the key features of
Hibernate.
Hibernate
features:
-
Object-Relational Mapping (ORM)
- Automatic
Table Generation
-
Transparent Persistence
- Caching
- Lazy
Loading
- HQL
(Hibernate Query Language)
- Second-level
Cache
3. What is the SessionFactory in
Hibernate?
`SessionFactory`
is a factory class that creates `Session` instances in Hibernate. It's a
thread-safe and immutable cache of compiled mappings for a single database. It
is costly to create, so typically, only one instance is created per
application.
4. Explain the Session in Hibernate.
`Session`
is a short-lived, lightweight object that represents a single-threaded unit of
work within Hibernate. It provides methods to perform CRUD (Create, Read,
Update, Delete) operations on the objects mapped to the database.
5. What is the difference between
save(), persist(), and saveOrUpdate() methods in Hibernate?
- `save()`
and `persist()` methods are used to save an object to the database. `save()`
returns the generated identifier, while `persist()` doesn't.
-
`saveOrUpdate()` updates an existing object or saves a new one if it doesn't
exist.
6. Explain the Hibernate caching
mechanisms.
Hibernate
provides two levels of caching:
- First-level
Cache: Session-level cache for entities, managed automatically by Hibernate within
a session.
- Second-level
Cache: Application-level cache shared across sessions, enhancing performance by
caching queries and entities.
7. What is lazy loading in
Hibernate?
Lazy
loading is a technique where the associated objects of a class are not loaded
unless they are explicitly accessed. It helps improve performance by reducing
unnecessary database access.
8. Explain the different mapping
types in Hibernate.
Hibernate
supports various mapping types, including:
- Primitive
types
-
Collection types (lists, sets, maps, arrays)
- Component
types
-
Associations (one-to-one, many-to-one, many-to-many)
9. What is the difference between
FetchType.LAZY and FetchType.EAGER in Hibernate?
-
`FetchType.LAZY` loads the associated objects when they are needed (on-demand),
improving performance.
-
`FetchType.EAGER` loads the associated objects immediately with the main
object, potentially causing performance issues.
10. What are the different ways to
perform mapping in Hibernate?
Hibernate
supports mapping through:
- XML
configuration files
-
Annotation-based mapping
-
Java-based mapping (using Java Persistence API annotations)
11. Explain the difference between
transient, persistent, and detached objects in Hibernate.
- Transient
objects are not associated with any Hibernate session.
-
Persistent objects are associated with a Hibernate session and managed by it.
- Detached
objects were once associated with a session but are no longer, although they
retain their state.
12. What is a detached object in
Hibernate?
A detached
object is an object that was once associated with a Hibernate session but has
been disconnected from the session. It is no longer managed by Hibernate, but
its state can be re-attached to another session.
13. What is HQL (Hibernate Query
Language)?
HQL is an
object-oriented query language in Hibernate used to perform database operations
on entities. It is similar to SQL but operates on the objects and properties of
Java classes.
14. Explain the concept of
transactions in Hibernate.
Transactions
in Hibernate are units of work that are atomic and consistent. A transaction
comprises multiple database operations that either succeed as a whole or fail
entirely. Hibernate uses the `Transaction` interface to manage transactions.
15. What is a named query in
Hibernate?
A named
query is a pre-defined query that is declared in the Hibernate mapping files or
annotated with `@NamedQuery` in the entity class. It allows you to reuse
queries by a name instead of writing them in the code.
16. Explain the concept of cascading
in Hibernate.
Cascading
in Hibernate allows operations (e.g., save, delete) to be cascaded from a
parent entity to its associated entities. For example, if a parent is saved,
its associated entities can be automatically saved as well.
17. What is a composite key in
Hibernate?
A composite
key, also known as a composite primary key, is a key composed of multiple
columns in a database table. In Hibernate, you can map a composite key using
`@Embeddable` and `@EmbeddedId` annotations.
18. What is a Hibernate interceptor?
A Hibernate
interceptor allows you to intercept and modify events (e.g., save, update,
delete) in Hibernate. You can use it to customize the behavior of Hibernate
before or after certain operations.
19. Explain the concept of dirty
checking in Hibernate.
Dirty
checking is a feature in Hibernate that automatically tracks changes made to
the persistent objects and synchronizes these changes with the database during
the transaction commit. It helps reduce manual update calls.
20. What are the differences between
Hibernate and JDBC?
- Hibernate
is an ORM framework, while JDBC is a low-level API for interacting with
databases.
- Hibernate abstracts database-related tasks and provides higher-level object-oriented APIs, while JDBC involves writing SQL queries and handling result sets directly.
Above are few top Hibernate interview questions. Remember to prepare and expand on these answers.
Good luck with your interview! 👍
0 Comments
Please share your comments ! Thank you !