Vidya Jyothi Institute of Technology (An Autonomous Institution)
Vidya Jyothi Institute of Technology (An Autonomous Institution)
BACHELOR OF TECHNOLOGY
IN
INFORMATION TECHNOLOGY
Submitted by
Vukkalker Nandini
17911A1258
CERTIFICATE
M.Tech, M.E.,
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 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:
1.2 Motivation:
CHAPTER 2
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.
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.
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
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
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.
11
Hibernate - Java Persistence Framework
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:
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.
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
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
15
Hibernate - Java Persistence Framework
16
Hibernate - Java Persistence Framework
CHAPTER 6
WORKING
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
18
Hibernate - Java Persistence Framework
19
Hibernate - Java Persistence Framework
6.4 Logging
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
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
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>
</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
23
Hibernate - Java Persistence Framework
CHAPTER 7
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.
• 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
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