Here is the formatted table with Hibernate interview questions and answers:
SNO | Question | Answer |
---|---|---|
1 | What is Hibernate? | Hibernate is an open-source ORM framework in Java. It simplifies database interaction by mapping Java objects to database tables and automating CRUD operations. |
2 | What are the main advantages of Hibernate over JDBC? | - No cumbersome JDBC boilerplate code - Supports automatic SQL query generation - Faster processing due to caching - Supports HQL (Hibernate Query Language) - Built-in transaction management |
3 | What is HQL (Hibernate Query Language)? | HQL is an object-oriented query language in Hibernate that uses entity names instead of table names. Example: java <br> Query query = session.createQuery("FROM Employee WHERE department = 'IT'"); <br> List<Employee> employees = query.list(); <br> |
4 | What are the different states of a Hibernate object? | - Transient – The object is not associated with any Hibernate session. - Persistent – The object is in a session and mapped to a database row. - Detached – The object was persistent but is now outside the session. |
5 | What is the difference between get() and load() in Hibernate? | - get() – Fetches data immediately; returns null if no record is found. - load() – Uses lazy loading; throws an exception if no record is found. |
6 | What is caching in Hibernate? | Caching improves performance by reducing database queries. - First-level cache – Enabled by default; stores data within a session. - Second-level cache – Shared across multiple sessions; requires explicit configuration. |
7 | What is the difference between save() and persist() in Hibernate? | - save() – Returns the generated identifier and inserts the record immediately. - persist() – Does not return an ID; insertion occurs only when the transaction commits. |
8 | What is the difference between Session and SessionFactory? | - SessionFactory – A heavyweight object that creates Session instances; spans the entire application. - Session – A lightweight object representing a unit of work; interacts with the database. |
9 | What are Hibernate Annotations? | Hibernate annotations allow entity mappings without XML. Example: java <br> @Entity <br> @Table(name = "employees") <br> public class Employee { <br> @Id <br> @GeneratedValue <br> private int id; <br> private String name; <br> } <br> |
10 | How does Hibernate handle transactions? | Hibernate transactions are managed using Transaction objects. Example: java <br> Transaction tx = session.beginTransaction(); <br> session.save(employee); <br> tx.commit(); <br> |
11 | What is the difference between merge() and update() in Hibernate? | - update() – Reattaches a detached object to the session and updates it. - merge() – Merges changes from a detached object into a persistent object copy. |
12 | What is Lazy and Eager loading in Hibernate? | - Lazy Loading – Data is not retrieved until needed. - Eager Loading – Related entities are retrieved immediately. Example: java <br> @OneToMany(fetch = FetchType.LAZY) // Lazy loading <br> @OneToMany(fetch = FetchType.EAGER) // Eager loading <br> |
13 | What are the different inheritance strategies in Hibernate? | Hibernate supports three inheritance mapping strategies: - Single Table Strategy – @Inheritance(strategy = InheritanceType.SINGLE_TABLE) - Joined Table Strategy – @Inheritance(strategy = InheritanceType.JOINED) - Table Per Class Strategy – @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) |
14 | What is the Criteria API in Hibernate? | The Criteria API allows dynamic queries without writing HQL. Example: java <br> Criteria criteria = session.createCriteria(Employee.class); <br> criteria.add(Restrictions.eq("department", "IT")); <br> List<Employee> employees = criteria.list(); <br> |
15 | What is the difference between openSession() and getCurrentSession()? | - openSession() – Always creates a new session. - getCurrentSession() – Returns an existing session if available. |
No comments