What is ORM ?

ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.

What does ORM consists of ?

An ORM solution consists of the followig four pieces:

API for performing basic CRUD operations
API to express queries refering to classes
Facilities to specify metadata
Optimization facilities : dirty checking,lazy associations fetching

What are the ORM levels ?

The ORM levels are:

Pure relational (stored procedure.)
Light objects mapping (JDBC)
Medium object mapping
Full object Mapping (composition,inheritance, polymorphism, persistence by reachability)

What is Hibernate ?

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:

Improved productivity
High-level object-oriented API
Less Java code to write
No SQL to write
Improved performance
Sophisticated caching
Lazy loading
Eager loading
Improved maintainability
A lot less code to write
Improved portability
ORM framework generates database-specific SQL for you

What Does Hibernate Simplify ?

Hibernate simplifies:

Saving and retrieving your domain objects
Making database column and table name changes
Centralizing pre save and post retrieve logic
Complex joins for retrieving related items
Schema creation from object model

How to configure Hibernate ?

Use below dependencies in the pom.xml file to use hibernate

Use below configuration file to use MySQL database

What are the most common methods of Hibernate configuration ?

The most common methods of Hibernate configuration are:

Programmatic configuration
XML configuration (hibernate.cfg.xml)

What are the Core interfaces are of Hibernate framework ?

The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.

Session interface
SessionFactory interface
Configuration interface
Transaction interface
Query and Criteria interfaces

What is SessionFactory in Hibernate ?

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 ?

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.

What is the general flow of Hibernate communication with RDBMS ?

The general flow of Hibernate communication with RDBMS is :

Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files
Create session factory from configuration object
Get one session from this session factory
Create HQL Query
Execute query to get list containing Java objects

What is query interface in Hibernate ?

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.