Hibernate Interview
Hibernate Interview
Hibernate simplifies database interactions in Java by providing an ORM framework. It handles database
operations without requiring extensive SQL code, offering portability, scalability, and a way to manage database
relationships using object-oriented principles.
2 What is a Framework?
A framework is a pre-defined structure that provides a standard way to build and deploy applications. It
offers reusable components, tools, and libraries that help developers write code more efficiently and consistently.
ORM (Object-Relational Mapping) is a programming technique for converting data between incompatible
systems (e.g., databases and object-oriented programming languages). Examples include Hibernate (Java), Entity
Framework (C#), and Django ORM (Python).
- `SessionFactory` is a factory for `Session` objects. It is a thread-safe, heavyweight object created once
during application startup and is usually associated with a single database. The design pattern used is the
**Factory Pattern**.
6. **What is a Session? Is there any limitation for the Session objects from SessionFactory?**
- A `Session` is a lightweight, non-thread-safe object that represents a single unit of work with the database.
You can create multiple `Session` objects from a `SessionFactory`, but they should not be shared between
threads.
7. **What is a Dialect?**
- In Hibernate, a dialect is a class that Hibernate uses to communicate with the database. It translates
Hibernate queries into SQL commands specific to the database being used (e.g., `MySQLDialect`,
`OracleDialect`).
- `<hibernate-configuration>`
- `<session-factory>`
- `<property name="hibernate.dialect">`
- `<mapping resource="...">`
- `<property name="hibernate.hbm2ddl.auto">`
- Yes, you can change the file name, but you must load the configuration programmatically using
`Configuration.configure("yourfilename.xml")`.
10. **Can we read or write data into the database without a transaction?**
- Technically, you can, but it is not recommended. Transactions ensure data consistency and integrity.
- **Hibernate**: Object-oriented, provides caching, lazy loading, and handles complex queries.
- `get()`: Returns null if the object is not found; it fetches the object eagerly.
- `load()`: Throws an exception if the object is not found; it fetches the object lazily.
- **One-to-One**
- **One-to-Many**
- **Many-to-One**
- **Many-to-Many**
14. **WAP to demonstrate `@OneToOne`, `@OneToMany`, `@ManyToOne`, `@ManyToMany`.**
- **Eager Loading**: Fetches related entities immediately with the main entity.
17. **WAP to demonstrate lazy and eager loading using relational mapping.**
- Java Persistence API (JPA) is a specification for ORM in Java, providing a standard way to manage
relational data in Java applications.
- `EntityManagerFactory` is a factory for creating `EntityManager` instances, used in JPA for managing
entities and interacting with the database.
- `EntityManager` is an interface used to interact with the persistence context, managing the life cycle of
entities in JPA.
- Hibernate caching improves performance by storing frequently accessed data in memory, reducing the
need to query the database.
- **First-Level Cache**: Automatically enabled, it caches objects within the same session.
- Hibernate Query Language (HQL) is an object-oriented query language similar to SQL but works with
Hibernate entities.
30. **What are the types of queries we can use in Hibernate? Explain.**
34. **What are methods used for performing CRUD operations in Hibernate?**
- **Delete**: `delete()`
35. **What are methods used for performing CRUD operations in JPA?**
- **Create**: `persist()`
- **Update**: `merge()`
- **Delete**: `remove()`