Java Frameworks:
In the context of Java programming model, there are 2 types of
technologies:
1. Specification
2. Framework
Specification:
It is a set of rules and it is further implemented by some 3rd party
software.
E.g.
Jakarta EE is a specification and it is implemented by 3rd party
software known as Web Server and Application Server.
Framework:
It is a partial solution used to address common problems.
It provides its own library and a workflow.
Depending upon the requirement, further customizations can be done.
There are several Java based frameworks available:
1. Struts – Developing Web Apps
2. JSF
3. Junit – Used for unit testing
4. Mockito – Used for Mocking
5. Wicket
Although there are several Java based frameworks available, 2 of
them are very popular:
1. Hibernate
2. Spring
Hibernate:
Hibernate is an open-source Java based framework used to building
Persistence Layer of an application.
JDBC Limitations:
1. A lot of coding (Boiler-Plate Code)
2. SQL is mandatory
3. No support for Object Modeling, needs extra efforts to have
it.
4. No support for Performance Optimization, needs extra efforts
to have it.
Hibernate can be used to overcome the limitations of JDBC.
It hides coding complexities and handles data persistency (CRUD)
using an underlying JDBC API.
Therefore, Hibernate is not a replacement to JDBC; rather it is an
abstraction on the top of JDBC.
Introduction to ORM:
ORM stands for Object to Relational Mapping.
It is a set of principles followed by several frameworks e.g.
Hibernate, Toplink, Eclipselink, IBatis and so on.
Principles of ORM:
1. Automated Persistence
2. Full Support for Object Modeling
3. Full Support for Performance Optimization
4. Supports a Query Language that works with classes and their
properties rather than tables and their columns
5. Works on the basis of Metadata.
Hibernate Core API:
The core API of Hibernate belongs to [Link] package.
1. Session
2. SessionFactory
3. Configuration
4. Transaction
5. Query
Session:
It is an interface from [Link] package.
It is a light-weight object and used for handling data persistency
(CRUD Operations).
It is an abstraction on the top of [Link].
SessionFactory:
It is an interface from [Link] package.
It is a heavy-weight object and used for obtaining the Session.
Since it is heavy-weight object, recommended to have only one per
application.
Configuration:
It is a class from [Link] package.
It is used to configure Hibernate and obtain SessionFactory.
Transaction:
It is an interface from [Link] package.
It maintains atomicity.
It is required especially in case of DML operations.
Query:
It is an interface from [Link] package.
It is used to perform several query operations.
Getting Started:
1. Setup Development Environment
In order to get started with Hibernate, it is necessary to
setup the development environment.
● JDK
● Any IDE (Eclipse for Ent Java)
● Any RDBMS (MySQL)
● Hibernate Libraries
Since Hibernate is a Java based framework, its
libraries are available in the form of .class files.
These .class files are bundled together in some .jar
(Java Archive) files.
These .jar files are required to be downloaded from
internet.
This can be done using 2 options:
● Manual Download
● Using Some Build Tool
2. Provide an Entity Class
In the context of ORM like Hibernate, any class that is
mapped to the corresponding table in the DB is referred as
an Entity Class.
3. Provide Mapping Metadata
Any ORM like Hibernate uses a metadata to map entity classes
to relational DB tables.
It is known as Mapping Metadata.
It is possible using 2 options:
● XML Based
● Annotation Based
In case of XML, the mapping file is referred as HBM file.
This file can have any name but by convention it is
[EntityClass].[Link].
This file must be placed under: src/main/resources folder.
4. Provide Configuration Metadata
Since Hibernate hides all the complexities e.g. Loading the
driver, Establishing Connection etc., it is necessary to
specify DB configuration setup.
There are 2 options available:
● Declaratively
● Programmatically
In case of declarative approach, there are further 2 options
PROPERTIES file and XML file.
In case of PROPERTIES file, Hibernate refers to a file named as
[Link].
In case of XML option, the file can have any name; but by default
Hibernate refers to a file named as [Link].
It is referred as CFG file.
This file must be placed under: src/main/resources folder.
5. Write Main Class
Hibernate Example Workflow:
When the object of Configuration class is created, Hibernate
searches for a configuration file named as [Link].
If found, Hibernate loads all the entries from that file.
If configuration specific properties are provided using an XML file,
it is necessary to intimate Hibernate about the same.
This is done using configure() method.
By default, Hibernate searches for [Link] file.
If the filename is something else, overloaded configure() method is
to be used.
Once Hibernate loads configuration specific information, further it
jumps to Mapping File i.e. HBM file.
It loads all the entries from HBM file and understands the mappings
between class and table, field and column etc.