Register Login

Hibernate Interview Questions

Hibernate Interview FAQ's

1. What is Hibernate?

A: Hibernate is lightweight open source ORM tool used to store, retrieve, and manipulate data from a database.

2. What is meant by ORM?

A: ORM or Object/Relational Mapping is a programming technique that maps objects to data stored in a database. Mapping makes creation, manipulation, and access simpler.

3. What core interfaces does Hibernate have?

A: Hibernate framework core interfaces include:

  • Configuration
  • SessionFactory
  • Session
  • Query
  • Criteria
  • Transaction

4. What is meant by SessionFactory?

A: SessionFactory is the factory and provides the instance of Session. It contains second level cache data which is not enabled by default.

5. What is meant by Session?

A: Session is what maintains the connection between the database and the Hibernate application. It provides methods for storing, fetching, updating, or deleting data from database.

6. In what states can objects exist in Hibernate?

A: Objects can exist in 3 states in Hibernate:

1.Transient: The object has just been created, but does not have a primary key or identifier, nor is it associated with a session.

2.Persistent: The object session is open and the instance has just been saved into or retrieved from the database.

3.Detached: When the object session is closed and has not yet been called.

7. What is meant by Hibernate configuration file?

A: The Hibernate configuration file has configurations specific to the database and is the properties file needed by application. It is the first object created and is usually created only once.

8. What strategies are used for inheritance mapping in Hibernate?

A: Inheritance strategies for hibernate include table per hierarchy, table per concrete class, and table per subclass.

9. What is meant by Lazy Loading?

A: In Hibernate, Lazy Loading is the phenomenon where objects are loaded on demand only. This improves the performance of the program by loading the child objects only when asked.

10. What does Automatic Dirty Checking mean?

A: Automatic dirty checking is a feature of hibernate that calls an update statement automatically on objects modified in transaction.

11. What does the Configuration Interface do?

A: The configuration interface is used to set up the Hibernate session being used by locating mapping documents. In other words, it bootstraps hibernate.

12. What is meant by POJO?

A: POJO or Plain Old Java Objects are Java objects with well defined get and set methods for all properties.

13. What is HQL?

A: The query language used in Hibernate is called Hibernate Query Language. It is an extension of SQL, but is a simpler, more efficient, and more flexible language to perform various operations on a database.

14. What is meant by JDBC?

A: JDBC or Java Database Connectivity gives a set of Java API to access relational databases from a Java program.

15. What does Transaction mean in hibernate?

A: A transaction in Hibernate is a unit of work done with a database. It is an optional object, and hibernate application may choose to not use it.

16. What does Query signify in Hibernate?

A: Query objects in Hibernate use HQL or SQL strings to pick up data from database and create objects.

17. What is meant by Criteria in Hibernate?

A: Criteria are objects that create and execute criteria queries to retrieve objects.

18. What method adds criteria to a query?

A: The method Session.createCriteria is used to create a new Criteria instance for a given class or the superclass of a class.

19. What method creates an HQL query?

A: The method Session.createQuery is used to create a new query instance for a given HQL query string.

20. What is meant by persistent class in Hibernate?

A: The classes of Java which have their objects or instance saved in database tables are persistent classes.

21. Where are Object/Relational Mappings defined in Hibernate?

A: Usually, mappings are defined in XML document. Map files serve as instruction to Hibernate on how to map class or classes to database.

22. What is meant by first level cache in Hibernate?

A: First Level Cache is a Session cache, and all requests must compulsorily pass through it. Before adding an object to the database, the Session object keeps it under its own control.

23. What is meant by second level cache in Hibernate?

A: The second level cache is the optional cache. The first level cache is consulted before an object is attempted to be located in the second level cache.

24. What is meant by Query level cache in Hibernate?

A: Close integrated with second level cache, the Query level cache is implemented by Hibernate for Query result sets. It is also an optional cache and needs two extra physical caches for cached query results and table update timestamps. They are useful for queries which are frequently run with same parameters.

25. How does Hibernate reduce database writing time?

A: The dirty checking feature of Hibernate reduces writing time by updating only the fields requiring changes and leaving other fields intact.

26. What is the function of callback interfaces in Hibernate?

A: Hibernate callback interfaces receive notifications for events from objects. An event generates when objects are loaded or deleted, for example, and notification for this event is sent via the callback interface.

27. What is meant by Light Object Mapping in Hibernate?

A: Light Object Mapping is a level of ORM where classes represent entities and are manually mapped.

28. What is meant by Derived Properties in Hibernate?

A: Derived Properties are properties not mapped to database table columns. They are calculated during runtime by evaluating expressions.