0% found this document useful (0 votes)
108 views11 pages

Hibernate Tutorial: 10.1introduction To Hibernate 3.0 10.2 Hibernate Architecture 10.3 First Hibernate Application

The document provides information about Hibernate, including: - Hibernate is an open source ORM tool that simplifies object-relational mapping and interaction between Java objects and a database. - The key components of the Hibernate architecture include the SessionFactory, Session, Transaction, and ConnectionProvider objects. Sessions are used to retrieve and save objects to the database.

Uploaded by

kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views11 pages

Hibernate Tutorial: 10.1introduction To Hibernate 3.0 10.2 Hibernate Architecture 10.3 First Hibernate Application

The document provides information about Hibernate, including: - Hibernate is an open source ORM tool that simplifies object-relational mapping and interaction between Java objects and a database. - The key components of the Hibernate architecture include the SessionFactory, Session, Transaction, and ConnectionProvider objects. Sessions are used to retrieve and save objects to the database.

Uploaded by

kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Section 10 : Hibernate

10.1Introduction to Hibernate 3.0


10.2 Hibernate Architecture
10.3 First Hibernate Application

Hibernate Tutorial

This hibernate tutorial provides in-depth concepts of Hibernate Framework with simplified
examples. It was started in 2001 by Gavin King as an alternative to EJB2 style entity bean.
The stable release of Hibernate till July 16, 2014, is hibernate 4.3.6. It is helpful for
beginners and experienced persons.

Hibernate Framework
Hibernate framework simplifies the development of java application to interact with the
database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.

An ORM tool simplifies the data creation, data manipulation and data access. It is a
programming technique that maps the object to the data stored in the database.

The ORM tool internally uses the JDBC API to interact with the database.

Advantages of Hibernate Framework


There are many advantages of Hibernate Framework. They are as follows:

1) Opensource and Lightweight: Hibernate framework is opensource 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 bydefault.

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: To fetch data form 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.

Next TopicHibernate Architecture

Hibernate Architecture
1. Hibernate Architecture

2. Elements of Hibernate Architecture

1. SessionFactory

2. Session

3. Transaction

4. ConnectionProvider

5. TransactionFactory

The Hibernate architecture includes many objects persistent object, session factory,
transaction factory, connection factory, session, transaction etc.

There are 4 layers in hibernate architecture java application layer, hibernate framework
layer, backhand api layer and database layer.Let's see the diagram of hibernate
architecture:
This is the high level architecture of Hibernate with mapping file and configuration file.
Hibernate framework uses many objects session factory, session, transaction etc. alongwith
existing Java API such as JDBC (Java Database Connectivity), JTA (Java Transaction API)
and JNDI (Java Naming Directory Interface).

Elements of Hibernate Architecture


For creating the first hibernate application, we must know the elements of Hibernate
architecture. They are as follows:

SessionFactory

The SessionFactory is a factory of session and client of ConnectionProvider. It holds second


level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory
method to get the object of Session.
Session

The session object provides an interface between the application and data stored in the
database. It is a short-lived object and wraps the JDBC connection. It is factory of
Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The
org.hibernate.Session interface provides methods to insert, update and delete the object. It
also provides factory methods for Transaction, Query and Criteria.

Transaction

The transaction object specifies the atomic unit of work. It is optional. The
org.hibernate.Transaction interface provides methods for transaction management.

ConnectionProvider

It is a factory of JDBC connections. It abstracts the application from DriverManager or


DataSource. It is optional.

TransactionFactory

It is a factory of Transaction. It is optional.

Next Topic Steps To Create First Hibernate Application

First Hibernate Example without IDE


1. Steps to create first Hibernate Application

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. Load the jar file

6. Run the first hibernate application without IDE

Here, we are going to create the first hibernate application without IDE. For creating the
first hibernate application, we need to follow following steps:

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. Load the jar file

6. Run the first hibernate application without IDE

1) Create the Persistent class


A simple Persistent class should follow some rules:

o A no-arg constructor: It is recommended that you have a default constructor at


least package visibility so that hibernate can create the instance of the Persistent
class by newInstance() method.

o Provide an identifier property (optional): It is mapped to the primary key


column of the database.

o Declare getter and setter methods (optional): The Hibernate recognizes the
method by getter and setter method names by default.

o Prefer non-final class: Hibernate uses the concept of proxies, that depends on the
persistent class. The application programmer will not be able to use proxies for lazy
association fetching.

Let's create the simple Persistent class:

Employee.java
1. package com.javatpoint.mypackage;
2.
3. public class Employee {
4. private int id;
5. private String firstName,lastName;
6.
7. public int getId() {
8. return id;
9. }
10. public void setId(int id) {
11. this.id = id;
12. }
13. public String getFirstName() {
14. return firstName;
15. }
16. public void setFirstName(String firstName) {
17. this.firstName = firstName;
18. }
19. public String getLastName() {
20. return lastName;
21. }
22. public void setLastName(String lastName) {
23. this.lastName = lastName;
24. }
25.
26.
27. }

2) Create the mapping file for Persistent class


The mapping file name conventionally, should be class_name.hbm.xml. There are many
elements of the mapping file.

o hibernate-mapping is the root element in the mapping file.

o class It is the sub-element of the hibernate-mapping element. It specifies the


Persistent class.

o id It is the subelement of class. It specifies the primary key attribute in the class.

o generator It is the subelement of id. It is used to generate the primary key. There
are many generator classes such as assigned (It is used if id is specified by the
user), increment, hilo, sequence, native etc. We will learn all the generator classes
later.

o property It is the subelement of class that specifies the property name of the
Persistent class.

Let's see the mapping file for the Employee class:

employee.hbm.xml
1. <?xml version='1.0' encoding='UTF-8'?>
2. <!DOCTYPE hibernate-mapping PUBLIC
3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5.
6. <hibernate-mapping>
7. <class name="com.javatpoint.mypackage.Employee" table="emp1000">
8. <id name="id">
9. <generator class="assigned"></generator>
10. </id>
11.
12. <property name="firstName"></property>
13. <property name="lastName"></property>
14.
15. </class>
16.
17. </hibernate-mapping>

3) Create the Configuration file


The configuration file contains informations about the database and mapping file.
Conventionally, its name should be hibernate.cfg.xml .

hibernate.cfg.xml
1. <?xml version='1.0' encoding='UTF-8'?>
2. <!DOCTYPE hibernate-configuration PUBLIC
3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5.
6. <hibernate-configuration>
7.
8. <session-factory>
9. <property name="hbm2ddl.auto">update</property>
10. <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
11. <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
12. <property name="connection.username">system</property>
13. <property name="connection.password">oracle</property>
14. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property
>
15. <mapping resource="employee.hbm.xml"/>
16. </session-factory>
17.
18. </hibernate-configuration>

4) Create the class that retrieves or stores the object


In this class, we are simply storing the employee object to the database.

1. package com.javatpoint.mypackage;
2.
3. import org.hibernate.Session;
4. import org.hibernate.SessionFactory;
5. import org.hibernate.Transaction;
6. import org.hibernate.cfg.Configuration;
7.
8. public class StoreData {
9. public static void main(String[] args) {
10.
11. //creating configuration object
12. Configuration cfg=new Configuration();
13. cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
14.
15. //creating seession factory object
16. SessionFactory factory=cfg.buildSessionFactory();
17.
18. //creating session object
19. Session session=factory.openSession();
20.
21. //creating transaction object
22. Transaction t=session.beginTransaction();
23.
24. Employee e1=new Employee();
25. e1.setId(115);
26. e1.setFirstName("sonoo");
27. e1.setLastName("jaiswal");
28.
29. session.persist(e1);//persisting the object
30.
31. t.commit();//transaction is commited
32. session.close();
33.
34. System.out.println("successfully saved");
35.
36. }
37. }

5) Load the jar file


For successfully running the hibernate application, you should have the hibernate4.jar file.

download the latest hibernate jar file. Some other jar files or packages are required such as

o cglib

o log4j

o commons

o SLF4J

o dom4j

o xalan

o xerces

download the required jar files for hibernate

6) How to run the first hibernate application without IDE


We may run this hibernate application by IDE (e.g. Eclipse, Myeclipse, Netbeans etc.) or
without IDE. We will learn about creating hibernate application in Eclipse IDE in next
chapter.

To run the hibernate application without IDE:

o install the oracle10g for this example.

o load the jar files for hibernate. (One of the way to load the jar file is copy all the jar
files under the JRE/lib/ext folder). It is better to put these jar files inside the public
and private JRE both.

o Now Run the StoreData class by java com.javatpoint.mypackage.StoreData


Note: You need to connect with the internet to run this example.

You might also like