Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

ORM

Session 1 : Hibernate Introduction

Dessign by: DieuNT1

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 1


COURSE INTRODUCTION

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher


2
Academy - Internal Use
Course Introduction - Agenda
 ORM
 Hibernate Overview
 Build a Hibernate Application
 Hibernate Association & Collection mapping
 Hibernate Configuration
 Hibernate Query Language (HQL)
 Criteria Queries
 Native SQL
 Transaction

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 3


Lecture 01

HIBERNATE INTRODUCTION

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher


4
Academy - Internal Use
What is ORM?
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
 Java ORM Frameworks:
Hibernate
Spring DAO
Open JPA
Mybatis
Toplink
43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 5
What is Hibernate?

 Hibernate is an Object-Relational Mapping(ORM) solution


for JAVA and it raised as 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.

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 6


Lecture 02

HIBERNATE ADVANTAGES

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher


7
Academy - Internal Use
Hibernate advantages
 Mapping Java classes to database tables using XML files
and without writing any line of code.
 Provides simple APIs for storing and retrieving Java objects
directly to and from the database.
 If there is change in Database or in any table then the only
need to change XML file properties.
 Support for Query Language
 Database dependent Code, support multi databases:
MySQL, MS SQL Server, Oracle…
 Optimize performance
 Automatic Versioning and Time Stamping

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 8


Lecture 03

HIBERNATE ARCHITECTURE

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher


9
Academy - Internal Use
High Level Architecture

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 10


Elements of Hibernate Architecture (1)
 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.

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 11


Elements of Hibearnate Architecture (2)
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.

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 12


Lecture 04

CORE OBJECTS

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher


13
Academy - Internal Use
Core Objects
 Hibernate framework uses many objects session factory, session,
transaction etc. along with existing Java API such as JDBC (Java
Database Connectivity), JTA (Java Transaction API) and JNDI (Java
Naming Directory Interface).

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 14


Lecture 05

CONFIGURATION

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher


15
Academy - Internal Use
Introduction
 How your Java classes relate to the database tables?
 Hibernate 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.
 We will consider XML formatted file hibernate.cfg.xml to
specify required Hibernate properties in all of examples.

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 16


Hibernate Properties
No. Properties & Description

1 hibernate.dialect
This property makes Hibernate generate the appropriate SQL for the chosen database.

2 hibernate.connection.driver_class
The JDBC driver class.
3 hibernate.connection.url
The JDBC URL to the database instance.
4 hibernate.connection.username
The database username.
5 hibernate.connection.password
The database password.
6 hibernate.connection.pool_size
Limits the number of connections waiting in the Hibernate database connection pool.

7 hibernate.connection.autocommit
Allows autocommit mode to be used for the JDBC connection.

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 17


Hibernate with SQL Server
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration xmlns=http://www.hibernate.org/xsd/hibernate-configuration
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<!-- Database connection settings >
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=SMS</property>
<property name="connection.username">sa</property>
<property name="connection.password">12345678</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
</session-factory> </hibernate-configuration>

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 18


Lecture 06

SESSION AND SESSIONFACTORY

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher


19
Academy - Internal Use
Session class
 A Session is used to get a physical connection with a
database.
 Persistent objects are saved and retrieved through a
Session object.
 The session objects should not be kept open for a long
time because they are not usually thread safe and they
should be created and destroyed them as needed.
 The main function of the Session is to offer, create, read,
and delete operations for instances of mapped entity
classes.

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 20


SessionFactory class
 SessionFactory is the factory class through which we get
sessions and perform database operations.
 SessionFactory provides three methods through which we
can get Session object:
 getCurrentSession(): method returns the session bound to the
context. Need to have configured in hibernate configuration file like
following:
<property name="current_session_context_class">thread</property>

It’s faster than opening a new session

 openSession(): method always opens a new session. We should


close this session object once we are done with all the database
operations.

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 21


SessionFactory class
 openStatelessSession(): method returns instance
of StatelessSession.
 StatelessSession in Hibernate does not implement first-level
cache and it doesn’t interact with any second-level cache.
 Collections are also ignored by a stateless session.
 It’s more like a normal JDBC connection and doesn’t provide any
benefits that come from using hibernate framework.

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 22


Session and SessionFactory

static {
String hibernate_cfg_path = "/hibernate.cfg.xml";

try {
Configuration configuration = new Configuration();

configuration.configure(hibernate_cfg_path);

sessionFactory = configuration.buildSessionFactory();

Session session = sessionFactory.getCurrentSession();


session.beginTransaction();

} catch (Throwable ex) {


logger.error("Session Factory could not be created." + ex);

throw new ExceptionInInitializerError(ex);


}
}

43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 23

You might also like