0% found this document useful (0 votes)
14 views5 pages

Hibernate Interview

Uploaded by

Manasa P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Hibernate Interview

Uploaded by

Manasa P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1 Why Hibernate?

 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.

3 What is ORM? Give examples.

 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).

4. **Explain the Hibernate Architecture (with diagram).**

- Hibernate architecture consists of the following components:

- **Configuration**: Loads configuration and mapping files.

- **SessionFactory**: A factory for `Session` objects.

- **Session**: A single-threaded object for interacting with the database.

- **Transaction**: Handles transaction management.

- **Query**: Executes database queries.

- **Criteria**: Allows for creating complex queries.

5. **Explain what is SessionFactory with the design pattern of SessionFactory.**

- `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`).

8. **Mention some tags that are used in the configuration file.**

- Common tags in `hibernate.cfg.xml` include:

- `<hibernate-configuration>`

- `<session-factory>`

- `<property name="hibernate.dialect">`

- `<mapping resource="...">`

- `<property name="hibernate.hbm2ddl.auto">`

9. **Can we change the default file name of `hibernate.cfg.xml`?**

- 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.

11. **What is the difference between Hibernate & JDBC?**

- **Hibernate**: Object-oriented, provides caching, lazy loading, and handles complex queries.

- **JDBC**: Low-level, manual handling of SQL queries and results.

12. **What is the difference between `get()` and `load()` in Hibernate?**

- `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.

13. **What is Relational Mapping? Types of mapping in Hibernate.**

- Relational mapping connects object-oriented concepts to relational databases. Types include:

- **One-to-One**

- **One-to-Many**

- **Many-to-One**

- **Many-to-Many**
14. **WAP to demonstrate `@OneToOne`, `@OneToMany`, `@ManyToOne`, `@ManyToMany`.**

- [Code snippets required for each relationship demonstration.]

15. **What is Lazy Loading and Eager Loading?**

- **Lazy Loading**: Fetches related entities only when accessed.

- **Eager Loading**: Fetches related entities immediately with the main entity.

16. **How to enable lazy and eager loading in Hibernate?**

- Lazy Loading: Set `fetch = FetchType.LAZY`.

- Eager Loading: Set `fetch = FetchType.EAGER`.

17. **WAP to demonstrate lazy and eager loading using relational mapping.**

- [Code snippet required showing lazy and eager loading in action.]

18. **What is JPA?**

- Java Persistence API (JPA) is a specification for ORM in Java, providing a standard way to manage
relational data in Java applications.

19. **What is EntityManagerFactory?**

- `EntityManagerFactory` is a factory for creating `EntityManager` instances, used in JPA for managing
entities and interacting with the database.

20. **What is EntityManager?**

- `EntityManager` is an interface used to interact with the persistence context, managing the life cycle of
entities in JPA.

21. **What is the difference between EMF and SessionFactory?**

- **EMF**: Used in JPA, it creates `EntityManager` instances.

- **SessionFactory**: Used in Hibernate, it creates `Session` instances.

- EMF is JPA-compliant, while SessionFactory is Hibernate-specific.

22. **What is Hibernate Caching?**

- Hibernate caching improves performance by storing frequently accessed data in memory, reducing the
need to query the database.

23. **What are the types of Hibernate caching?**

- **First-Level Cache**: Session-level, default cache.

- **Second-Level Cache**: SessionFactory-level, optional.


- **Query Cache**: Cache for query results.

24. **Explain 1st level and 2nd level caching in detail.**

- **First-Level Cache**: Automatically enabled, it caches objects within the same session.

- **Second-Level Cache**: Configurable, it caches objects across multiple sessions.

25. **Can multiple sessions share 1st level cache memory?**

- No, the first-level cache is specific to a single session.

26. **What is Second Level Cache and how to enable it?**

- Second-Level Cache is shared across sessions. Enable it using the


`hibernate.cache.use_second_level_cache` property and configure a cache provider.

29. **What is HQL?**

- 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.**

- **HQL Query**: Object-oriented queries.

- **Native SQL Query**: Direct SQL queries.

- **Criteria Query**: Programmatic queries for dynamic query generation.

34. **What are methods used for performing CRUD operations in Hibernate?**

- **Create**: `save()`, `persist()`

- **Read**: `get()`, `load()`

- **Update**: `update()`, `merge()`

- **Delete**: `delete()`

35. **What are methods used for performing CRUD operations in JPA?**

- **Create**: `persist()`

- **Read**: `find()`, `getReference()`

- **Update**: `merge()`

- **Delete**: `remove()`

36. **Can we change the name of `persistence.xml` file in JPA?**

- No, `persistence.xml` is a standard name required by JPA.


37. **What is CriteriaBuilder?**

- `CriteriaBuilder` is an API used to build dynamic queries in JPA.

38. **What are the differences between Criteria and CriteriaBuilder?**

- **Criteria**: Hibernate-specific API for dynamic queries.

- **CriteriaBuilder**: JPA-compliant API for dynamic query creation.

You might also like