You are on page 1of 25

A TECHNICAL SEMINAR REPORT ON

“Hibernate-Java Persistence Framework”

In partial fulfillment of the requirements for the award of the degree of

BACHELOR OF TECHNOLOGY
IN
INFORMATION TECHNOLOGY

Submitted by
Vukkalker Nandini
17911A1258

Under the Esteemed Guidance of


Mr. M. Suresh Babu
Assoc. Professor

DEPARTMENT OF INFORMATION TECHNOLOGY


VIDYA JYOTHI INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
(Accredited by NBA, Approved by AICTE, Affiliated to JNTU Hyderabad)
Aziz Nagar Gate, C.B.Post, Chilkur Road, Hyderabad – 500075
2020 - 2021
VIDYA JYOTHI INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
(Accredited by NBA, Approved by AICTE, Affiliated to JNTU Hyderabad)
Aziz Nagar Gate, C.B.Post, Chilkur Road, Hyderabad - 500075

DEPARTMENT OF INFORMATION TECHNOLOGY

CERTIFICATE

This is to certify that the Technical Seminar Report on “Hibernate-Java Persistence


Framework” is a bonafide work by Vukkalker Nandini (17911A1258) in partial fulfillment of
the requirement for the award of the degree of Bachelor of Technology in “INFORMATION
TECHNOLOGY” JNTU Hyderabad during the year 2020 - 2021.

Technical Seminar Incharge Head of the department


Mr. M. Suresh Babu, Mr. B. Srinivasulu,

M.Tech, M.E.,

Associate Professor. Professor.


Contents
Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i

1 Introduction 1-6
1.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
1.2 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Introduction 6
7
2.1 What is Hibernate? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 What is ORM ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
2.3 Why ORM ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 7Block Diagram to Hibernate
8
2.4 Java ORM Framework . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1 Block Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7 8
3.2 Advantages of Hibernate Framework . . . . . . . . . . . . . . . . . . .
4 Hibernate 9 3.3 Supported Databases
Architecture 10
. . . . . . . . . . . . . . . . . . . . . . . . . . 9
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
4.1 Configuration Object . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
4.2 Session Factory Object . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
4.3 Session Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
5. 4.4 Transaction
Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Architecture .
12
5.1 Another View. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.5
14 Query Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
5.2 Project Configuration for Hibernate . . . . . . . . . . . . . . . . . . . . . .
4.6
15 Criteria Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
5.3 HQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
6. Working 17
6.1 Required Hibernate Libraries . . . . . . . . . . . . . . . . . . . . . . . .
17
6.2 Creating a Hibernate Library in Eclipse . . . . . . . . . . . . . . . . . .
18
6.3 hibernate.cfg.xml for MYSQL . . . . . . . . . . . . . . . . . . . . . . . .
19
6.4 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
6.5 Annotation Based Mapping . . . . . . . . . . . . . . . . . . . . . . . . .
7 21Advantages and drawbacks 24
6.6 Create Application Class . . . . . . . . .
7.1 Advantages of Hibernate Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2422
6.7 Drawbacks.
7.2 Execution .. .. .. .. .. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. .. . . . .. .. .. .. .. .. .
23
24
8 6.8Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. .. . . . . .
Applications
7.3
2523 Future Enhancement . . . . . . . . . . . . . . . . . . . . . . . . . .. . . .
9
25 References . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . .. . .
25
Hibernate - Java Persistence Framework

ABSTRACT

Hibernate - Java Persistence Framework

Hibernate is a framework which provides some abstraction layer, meaning that the programmer
does not have to worry about the implementations, Hibernate does the implementations for you
internally like Establishing a connection with the database, writing query to perform CRUD
operations etc. It is a java framework which is used to develop persistence logic. Persistence logic
means to store and process the data for long use. More precisely Hibernate is an opensource, non-
invasive, light-weight java ORM(Object-relational mapping) framework to develop objects
which are independent of the database software and make independent persistence logic in all
JAVA, JEE. Framework means it is special install-able software that provides an abstraction layer
on one or more technologies like JDBC, Servlet, etc to simplify or reduce the complexity for the
development process.

i
Hibernate - Java Persistence Framework

CHAPTER 1
INTRODUCTIO
N

1.1 Objective:

Hibernate ORM (or simply Hibernate) is an object–


relational mapping tool for the Java programming language.
It provides a framework for mapping an object-
oriented domain model to a relational database. Hibernate
handles object–relational independence mismatch problems
by replacing direct, persistent database accesses with high-
level object handling functions.
Hibernate is free software that is distributed under the GNU
Lesser General Public License 2.1.
Hibernate's primary feature is mapping from Java classes
to database tables, and mapping from Java data types to SQL
data types. Hibernate also provides data query and retrieval
facilities. It generates SQL calls and relieves the developer
from the manual handling and object conversion of the result
set.

1.2 Motivation:

Any enterprise application performs database operations by


storing and retrieving vast amounts of data. Despite all the
available technologies for storage management, application
developers normally struggle to perform database operations
efficiently. Generally, Java developers use lots of code or
proprietary framework to interact with the database.
Therefore, it is advisable to use Object Relational Mapping
(ORM) to reduce the burden of interacting with the database.
ORM forms a bridge between object models (Java program)
and relational models (database program) like JDBC.

It is a java framework which is used to develop persistence


logic. Persistence logic means to store and process the data
for long use. More precisely Hibernate is an open-source,
non-invasive, light-weight java ORM(Object-relational
mapping) framework to develop objects which are
independent of the database software and make independent
persistence logic in all JAVA, JEE.
6
Hibernate framework support Auto DDL operations. In
JDBC manually we have to create table and declare the data-
type for each and every column. But Hibernate can do DDL
Hibernate - Java Persistence Framework

CHAPTER 2

2.1 What is Hibernate

Hibernate is an Object-Relational Mapping(ORM) solution for JAVA. It is an open-source


persistent framework created by Gavin King in 2001. It is a powerful, high-performance Object-
Relational Persistence and Query service for any Java Application. Hibernate maps Java classes
to database tables and from Java data types to SQL data types and relieves the developer from
95% of common data persistence related programming tasks. Hibernate sits between traditional
Java objects and database server to handle all the works in persisting those objects based on the
appropriate O/R mechanisms and patterns.

2.2 What is ORM

ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting
data between relational databases and object-oriented programming languages such as Java, C#,
etc.

2.3 Why Object Relational Mapping (ORM)?

When we work with an object-oriented system, there is a mismatch between the object model and
the relational database table. RDBMSs represent data in a tabular format whereas object- oriented
languages, such as Java or C# represent it as an interconnected graph of objects.

Example One may encounter a few problems when direct interaction takes place between object
models and relation database tables. The following example highlights the problems associated
with traditional database application structure.

2.4 Java ORM Frameworks

There are several persistent frameworks and ORM options in Java. A persistent framework is an
ORM service that stores and retrieves objects into a relational database.
• Enterprise JavaBeans Entity Beans
• Java Data Objects
• Castor
• TopLink
• Spring DAO
• Hibernate, and many more

7
Hibernate - Java Persistence Framework

CHAPTER 3

BLOCK DAIGRAM

Hibernate is an Object-Relational Mapping(ORM) solution for JAVA. It is an open-source


persistent framework created by Gavin King in 2001. It is a powerful, high-performance Object-
Relational Persistence and Query service for any Java Application.
Hibernate maps Java classes to database tables and from Java data types to SQL data types and
relieves the developer from 95% of common data persistence related programming tasks.
Hibernate sits between traditional Java objects and database server to handle all the works in
persisting those objects based on the appropriate O/R mechanisms and patterns.

3.1 Hibernate Advantages

Here we have listed down the advantages of using Hibernate:


 Hibernate takes care of mapping Java classes to database tables using XML files and
without writing any line of code. If there is a change in the database or in any table, then
all that you need to change are the XML file properties.
 Provides simple APIs (classes and methods) for storing and retrieving Java objects
directly to and from the database.
 Hibernate supports Inheritance, Association relations, and Collections.

 Abstracts away the unfamiliar SQL types and provides a way to work around familiar
Java Objects.
 Hibernate does not require an application server to operate.
8
Hibernate - Java Persistence Framework
e

 Hibernate Supports only unchecked exceptions, so no need to write try, catch, or


throws blocks. Generally we have a Hibernate translator which converts Checked
exceptions to Unchecked.
 Minimizes database access with smart fetching strategies.
 Hibernate has its own query language. That is Hiberate Query Language (HQL)
which contains database independent controlers.
 Manipulates Complex associations of objects of your database.
 Hibernate supports caching mechanism: It reduces the number of round trips
(transactions) between an application and the database. It increases the application
performance.
 Hibernate supports annotations, apart from XML..

3.2 Supported Databases


Hibernate supports almost all the major RDBMS database servers. Following is a list of few
of the database engines that Hibernate supports:
 HSQL Database Engine
 DB2/NT
 MySQL
 PostgreSQL
 FrontBase
 Oracle
 Microsoft SQL Server Database
 Sybase SQL Server
 Informix Dynamic Server
Hibernate supports a variety of other technologies as well including:
 XDoclet Spring
 J2EE
 Eclipse plug-ins

 Maven

9
Hibernate - Java Persistence Framework

CHAPTER 4

HIBERNATE ARCHITECTURE

Hibernate has a layered architecture which helps the user to operate without having to know the
underlying APIs. Hibernate makes use of the database and configuration data to provide
persistence services (and persistent objects) to the application.
Following is a very high level view of the Hibernate Application Architecture.

Following is a detailed view of the Hibernate Application Architecture with a few important core
classes in the Hibernate layer.

10
Hibernate - Java Persistence Framework

Hibernate uses various existing Java APIs, like JDBC, Java Transaction API (JTA), and Java
Naming and Directory Interface (JNDI). JDBC provides a rudimentary level of abstraction
of functionality common to relational databases, allowing almost any database with a JDBC
driver to be supported by Hibernate. JNDI and JTA support Hibernate to be integrated with
J2EE application servers.
The following section gives a brief description about the class objects which are involved in
the Hibernate Layer of the given architecture diagram. This sectiong gives you a theritical
idea of how the hibernate class objects are used to build an application.

4.1 Configuration Object


Configuration is a serializable class. It is the first Hibernate object that you need to create
in any Hibernate application. It is usually created only once during application initialization.
It allows the application to specify properties and mapping documents to be used. The
Configuration object provides two keys components:
 Database Connection: A database connection is most important for Enterprise and
Database applications. It is handled through one or more configuration files
supported by Hibernate. Those are hibernate.properties file and hibernate.cfg.xml
file.
 Mapping Setup: This component creates the connection between the Java classes
and database tables. It creates mapping between each entity java class and each table
in the database.

11
Hibernate - Java Persistence Framework

4 . 2 Session Factory Object

SessionFactory is a Factory Interface used to create Session instances. After adding the
properties and Mapping files to the Configuration object, it is used to create a
SessionFactory object which in turn configures Hibernate (Front-end javaclasses and Back-
end tables) for the application. SessionFactory is a thread-safe object and used by all the
threads of an application. It is a heavyweight object, usually created during application
start-up and kept for later use. You would need one SessionFactory object per database
using a separate configuration file. So, if you are using multiple databases, then you would
have to create multiple SessionFactory objects.

4 . 3 Session Object
Session is an Interface that wraps the JDBC connection. That means, it creates a physical
connection between the application and a database. The Session object is lightweight and
designed to be instantiated each time an interaction is needed with the database. Persistent
objects are saved and retrieved through a Session object.
The Lifecycle of a Session is bounded by the beginning and end of a logical trasaction. It
contains three states:

 transient: never persistent, currently not associated with any Session.

 persistent: currently associated with unique Session.

 detached: previously persistent, currently not associated with any Session.

The session objects should not be kept open for a long time because they are not usually
thread-safe. They should be created and destroyed them as needed.

4.4 Transaction Object


Transaction is an Interface and it represents a unit of work with the database and most of
the RDBMS supports transaction functionality. Transactions in Hibernate are handled by an
underlying transaction manager.
This is an optional object and Hibernate applications may choose not to use this interface,
instead managing transactions in their own application code.

12
Hibernate - Java Persistence Framework

4 . 5 Query Object
Query is an interface and it is used in SQL or Hibernate Query Language (HQL) string to
retrieve data from the database and create objects. A Query instance is used to bind query
parameters, limit the number of results returned by the query, and finally to execute the
query.

4 .6 Criteria Object
Criteria is an interface and it is used for retrieving entity data by composing Criterion
(Interface) objects. Criterion Objects work like a condition (WHERE and IF) in the SQL
query, all the criterion objects (conditions) are added to the Criteria Object and that object
will be executed and used for retriving entity data in objects.

CHAPTER 5

APPLICATION ARCHITECTURE

13
Hibernate - Java Persistence Framework

5.1 Another View

Hibernate uses various existing Java APIs, like JDBC, Java Transaction API(JTA), and Java
Naming and Directory Interface (JNDI). JDBC provides a rudimentary level of abstraction of
functionality common to relational databases, allowing almost any database with a JDBC driver to
be supported by Hibernate. JNDI and JTA allow Hibernate to be integrated with J2EE application
servers.
Following section gives brief description of each of the class objects involved in Hibernate
Application Architecture.

14
Hibernate - Java Persistence Framework

5.2 Project Configuration for Hibernate

EventManager/ the project base directory


src/
hibernate.cfg.xml Hibernate configuration file
log4j.properties Log4J configuration file
eventmgr/ base package is "eventmgr"
domain/
Location.java
location.hbm.xml O-R mapping file for a class
build/
hibernate.cfg.xml copied here by IDE during build
log4j.properties copied here by IDE during build
eventmgr/
domain/
Location.class
location.hbm.xml copied here by IDE during build
lib/
hibernate3.jar Hibernate requires several jars
asm.jar
...
Hibernate requires to know in advance — where to find the mapping
information that defines how your Java classes relate to the database tables.
Hibernate also requires a set of configuration settings related to database and
other related parameters. All such information is usually supplied as a standard
Java properties file called hibernate properties, or as an XML file
named hibernate.cfg.xml.
I will consider XML formatted file hibernate.cfg.xml to specify required
Hibernate properties in my examples. Most of the properties take their default
values and it is not required to specify them in the property file unless it is
really required. This file is kept in the root directory of your application's
classpath.

15
Hibernate - Java Persistence Framework

5.3 HQL(Hibernate Query Language

Hibernate Query Language (HQL) is an object-oriented query language, similar to


SQL, but instead of operating on tables and columns, HQL works with persistent
objects and their properties. HQL queries are translated by Hibernate into
conventional SQL queries, which in turns perform action on database.
Although you can use SQL statements directly with Hibernate using Native SQL,
but I would recommend to use HQL whenever possible to avoid database
portability hassles, and to take advantage of Hibernate's SQL generation and
caching strategies.
Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but
properties like table and column names are case sensitive in HQL.

Query query = session.createQuery(


"from Event where name='Java
Days'");

Query query = session.createQuery(


"select e from Event e where
e.name='Java Days'");

 Hibernate queries you Hibernate Query Language (HQL).


 HQL is object-centric - use class and property names, not SQL table names.

16
Hibernate - Java Persistence Framework

CHAPTER 6

WORKING

6.1 Required Hibernate Libraries


Libraries are in lib/ directory of Hibernate distribution.
In your IDE:
create a Hibernate "library"
add JARs to the library
better than copying JAR files to your project

hibernate3.jar
antlr-2.7.6.jar
asm.jar
cglib.jar
commons-collections.jar
commons-logging.jar
dom4j.jar
ehcache.jar
jta.jar
log4j.jar
// and maybe
xml-apis.jar

17
Hibernate - Java Persistence Framework

6.2 Create a Hibernate Library in


Eclipse
1. Project -> Properties -> Java Build Path
2. select Libraries tab.
3. Click "Add Library..." and select "User Library", Next>
4. Create a new user library named "Hibernate"
5. Add Jar files

18
Hibernate - Java Persistence Framework

6.3 hibernate.cfg.xml for MySQL

<?xml version='1.0' encoding='utf-8'?>


<!DOCTYPE hibernate-configuration PUBLIC ... remainder omitted
>
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect </property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver </property>
<property name="connection.username">student</property>
<property name="connection.password">secret </property>
<property name="connection.url">
jdbc:mysql://se.cpe.ku.ac.th/eventmgr </property>
<!-- Object-Relational mappings for classes -->
<mapping resource="eventmgr/domain/Location.hbm.xml"/>
... other mappings omitted
</session-factory>
</hibernate-configuration>

19
Hibernate - Java Persistence Framework

6.4 Logging

Hibernate apps will log errors and/or activity.


Two choices:
Java SDK logging (since JDK 1.4)
Log4j
if you use Log4j, create a log4j.properties in your
application source root directory.
Copy this file from the Hibernate etc/ directory.

Example
[hibernate.dir]/doc/tutorial/src/log4j.properties
Configuration logging of Hibernate:
log4j.logger.org.hibernate=warn
Log the SQL generated by Hibernate:
#log4j.logger.org.hibernate.SQL=debug
Log JDBC bind parameters (can be security leak):
log4j.logger.org.hibernate.type=info

20
Hibernate - Java Persistence Framework

6.5 Annotation Based Mapping

Hibernate Annotations is the powerful way to provide the metadata for the Object and
Relational Table mapping. All the metadata is clubbed into the POJO java file along with
the code, this helps the user to understand the table structure and POJO simultaneously
during the development.
If you going to make your application portable to other EJB 3 compliant ORM applications,
you must use annotations to represent the mapping information, but still if you want greater
flexibility, then you should go with XML-based mappings.

Following is the mapping of Employee class with annotations to map objects with the
defined EMPLOYEE table −

import javax.persistence.*;
@Entity @Table(name = "EMPLOYEE")
public class Employee
{
@Id @GeneratedValue @Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "salary")
private int salary;

Hibernate detects that the @Id annotation is on a field and assumes that it should access
properties of an object directly through fields at runtime. If you placed the @Id annotation
on the getId() method, you would enable access to properties through getter and setter
methods by default. Hence, all other annotations are also placed on either fields or getter
methods, following the selected strategy.
Following section will explain the annotations used in the above class.

21
Hibernate - Java Persistence Framework

6.6 Create Application Class


Finally, we will create our application class with the main() method to run the application.
We will use this application to save few Employee's records and then we will apply CRUD
operations on those records.

Database Configuration
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<property name = "hibernate.dialect">


org.hibernate.dialect.MySQLDialect
</property>

<property name = "hibernate.connection.driver_class">


com.mysql.jdbc.Driver
</property>

<!-- Assume students is the database name -->

<property name = "hibernate.connection.url">


jdbc:mysql://localhost/test
</property>

<property name = "hibernate.connection.username">


root
</property>

<property name = "hibernate.connection.password">


cohondob
</property>

</session-factory>
</hibernate-configuration>

22
Hibernate - Java Persistence Framework

6.7 Execution
Here are the steps to compile and run the above mentioned application. Make sure, you have
set PATH and CLASSPATH appropriately before proceeding for the compilation and
execution.
•Delete Employee.hbm.xml mapping file from the path.
•Create Employee.java source file as shown above and compile it.
•Create ManageEmployee.java source file as shown above and compile it.
•Execute Manage Employee binary to run the program.
You would get the following result, and records would be created in EMPLOYEE table.

Applications

The web application uses the Hibernate framework as the persistence layer for retrieving


and storing plain old Java objects (POJOs) to a relational database. Hibernate is framework
that provides tools for object relational mapping (ORM).

23
Hibernate - Java Persistence Framework

CHAPTER 7

7 . 1 Advantages of Hibernate Framework


Following are the advantages of hibernate framework:

1)Open Source and Lightweight Hibernate framework is open source under the LGPL license and
lightweight.

2)Fast Performance The performance of hibernate framework is fast because cache is internally
used in hibernate framework. There are two types of cache in hibernate framework first level cache
and second level cache. First level cache is enabled by default.

3)Database Independent Query HQL (Hibernate Query Language) is the object- oriented version of
SQL. It generates the database independent queries. So you don’t need to write database specific
queries. Before Hibernate, if database is changed for the project, we need to change the SQL query
as well that leads to the maintenance problem.

4)Automatic Table Creation Hibernate framework provides the facility to create the tables of the
database automatically. So there is no need to create tables in the database manually.

5)Simplifies Complex Join Fetching data from multiple tables is easy in hibernate framework.

6)Provides Query Statistics and Database Status Hibernate supports Query cache and provide
statistics about query and database status.

7.2 Drawbacks of Hibernate

• Performance Cost
• Does not allow multiple inserts
• More Complex with joins
• Poor performance in Batch processing
• Not good for small project

24
Hibernate - Java Persistence Framework

7.3 Future Enhancement

 Implementation of a metadata index based search mechanism Implementation of a more


powerful database (My Sql) Implementation of an administration side which gives special
privileges to the administrator Implementation of an engine which allows the users to create
metadata tables dynamically. Implementation of a new mechanism to plot the graphs (removing
applets).

8. Conclusion

Hibernate package is a very powerful tool to perform caching operations. It makes the life of
programmers easier and the life of users faster. Through the successful implementation of the
“Web Scope”, it is now possible for the researchers to quickly access the MDS Plus datasets
through a web browser.

9. References

https://www.tutorialspoint.com/hibernate/hibernate_criteria_queries.htm
https://hibernate.org/
https://en.wikipedia.org/wiki/Hibernate_(framework)
https://docs.jboss.org/hibernate/stable/orm/userguide/html_single/Hibernate_User_Guide
.html
https://github.com/hibernate
https://www.youtube.com/watch?v=tgmeDUPhyuM
https://www.javatpoint.com/jpa-object-relational-mapping
https://stackoverflow.com/questions/452385/what-java-orm-do-you-prefer-and-why
https://en.wikipedia.org/wiki/List_of_object%E2%80%93relational_mapping_software
https://dzone.com/articles/best-java-orm-frameworks-for-postgresql

25

You might also like