Java framework that simplifies the development of Java application to interact with the database
Hibernate
Open source
Lightweight
ORM (Object Relational Mapping) tool
Implements the specifications of JPA (Java Persistence API) for data persistence
ORM tool
Technique that maps the object stored in the database
Simplifies data creation, manipulation, and access
Internally uses the Java API to interact with the databases
JPA
Defines a set of functionalities, standards, and concepts to the ORM tool
Available in the javax.persistence package
Allows interaction with the database by decreasing the line of codes for relational object management
Why Hibernate
Overcomes the database dependency faced in the JDBC
Strengthens the object level relationship - eliminates the impedance mismatch problem
Hibernate
Lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping
Uses persistent classes for data transfer between java application and databases
Hibernate
ORM tool which helps in the interaction between the java classes and relational database
Solves the problem of data mismatch found in Java application and RDBMS
Hibernate
Has its own query language, i.e., HQL (Hibernate Query Language) which is independent of the database
HQL is an object-oriented language similar to SQL, but it works with the persistent object and its properties
Hibernate Features
Auto-Generation
Lazy Loading
Database Independent
Auto-Generation
Hibernate provides a feature of automatic table generation. It means a programmer need not worry about the query implementation, i.e., Hibernate does on its own.
Lazy Loading
Hibernate supports a new concept called lazy loading. Lazy loading concept retrieves the only necessary object for execution.
It also improves the performance of an application.
Database Independent
Hibernate is database-independent as it provides 'DatabaseDialect' so we need not write SQL queries.
It supports many databases such as Oracle, MySql, Sybase, etc.
Hibernate Architecture
Layered architecture with 3 main layers: Application Layer, Hibernate Core Layer, Database Layer
Core Objects of the Hibernate Framework
Configuration object
SessionFactory object
Session object
Transaction object
Query object
Criteria object
Configuration object
Consists the configurationfile used by the hibernate. It mainly consist the information about databaseconnection (hibernate.cfg.xml) and classmapping (.hbm.xml). Configuration object is used to create the object of SessionFactory.
SessionFactory object
Created from the Configuration object. It acts as a factory for sessionobjects and client for ConnectionProvider.
Session object
Created from the SessionFactory object. It acts as a factory for Transaction, Query and Criteria. Session objects are not thread-safe. It maintains the first level cache.
Transaction object
Created from Session object. A transaction object represents an atomic unit of work.
Query object
Created from Session object. It uses SQL and HQL to perform database operations.
Criteria object
Created from the Session object. It is used to define and execute the object oriented criteria queries.
First Hibernate Example
1. Create the Persistent class
2. Create the mapping file for Persistent class
3. Create the Configuration file
4. Create the class that retrieves or stores the persistent object
5. Build and Run the application
Persistent class
A no-arg constructor
Provide an identifier property
Declare getter and setter methods
Mapping file name conventionally should be class_name.hbm.xml
Elements of the mapping file
hibernate-mapping
class
id
generator
property
Configuration file name conventionally should be hibernate.cfg.xml
Configuration file contains information about the database and mapping file
For MySQL database, the configuration file should use org.hibernate.dialect.MySQL5Dialect, com.mysql.jdbc.Driver, jdbc:mysql://localhost:3306, root, root
Entity Association Mapping
Mapping of associations between entity classes and the relationships between tables
Hibernate provides 4 types of association mapping: One To One, One To Many, Many To One, Many To Many
The types of association mapping can be uni-directional or bi-directional