Hibernate is Java-based middleware designed to complete the Object Relational (O/R) mapping model. In fact, one of the most complex parts of many O/R mapping mechanisms—writing SQL code—can be simplified significantly with Hibernate. With
Hibernate, you can handle the data in the object and not worry about the details of extracting, storing, and loading individual data components. Hibernate sits between traditional Java objects and handles all the work in persisting those objects based on the appropriate O/R mechanisms and patterns. Hibernate can persist even the most complex classes, including those using composition, inheritance, and collections.

Why do you need ORM tools like hibernate ?

The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:

SessionFactory is a singleton class, that means, only one instance will be created per application per JVM. The SessionFactory configures Hibernate for the application using the supplied configuration file(hibernate.cfg.xml) and allows for a Session object to be instantiated.

What is Session in Hibernate ?

Session session = sessionFactory.openSession();

The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. When the Session object is created, an object or two is persisted or loaded from the database, then the Session object is closed. When a Session object is instantiated, a connection is made to the database; thus the interactions should be fairly quick to ensure the application doesn’t keep database connections open needlessly.

Generally, a Session object isn’t created when the application is started and closed when the application exits; instead, it’s created when needed and closed when a persistence operation has completed.

It plays the following role

Wraps a JDBC connection
Factory for Transaction
Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier

What is transaction interface in Hibernate ?

The Transaction interface lets you make sure that all the operations on persisted objects occur in a transaction supported by either JDBC or JTA. When you’re using a transaction, any error that occurs between the start and end of the transaction will cause all the operations to fail; this is important when the persisted objects rely on each other to maintain integrity in the database.

The Query interface lets you query the database for objects instead of having to rely solely on an object’s identifier.

What are Hibernate Callbacks ?

When Hibernate is in the process of persisting or loading an object, it provides support for you to be warned of certain events so you can respond appropriately. The process is implemented through callbacks. The available events are as follows:

public Boolean onSave(Session s) – called before the object is saved to the database
public Boolean onUpdate(Session s) – called before the object is updated
public boolean onDelete(Session s) – called before the object is deleted
public Boolean onLoad(Session s) – called after the object is loaded from the database

What is Validatable interface in Hibernate ?

Hibernate also implements a Validatable interface that you can use to verify the state of an object before it is persisted to the database. The goal of the interface is not to change the state of the object but to make sure the object is in a state appropriate to be stored; otherwise an exception is thrown.

What is XML mapping document in Hibernate ?

The mapping document is an XML document, and it includes the necessary tags to indicate its status. The root element is <hibernate-mapping>; it contains all the <class> elements used to define specific mappings from a Java class to the database. If you don’t include a package attribute in the <hibernate-mapping> element, you must provide a fully qualified class name in the <class> element.

What is class element in Hibernate mapping document ?

The <class> element includes two attributes:

name: Identifies the fully qualified class name for the class or interface being mapped; if the package prefix is used in the <hibernate-mapping> element, the class name can be the unqualified name.

table: Specifies the name of the table in the database where the mapped class will be persisted.