Anda di halaman 1dari 29

Hibernate 3.

1
Siddhanta Kumar Pattnaik
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Agenda

JDBC Overview
Features of Hibernate
Core Objects Of Hibernate
Hibernate Associations Mapping
Hibernate Object state
Hibernate Connection Management
Hibernate Transaction Management
Hibernate Cache implementation
Caching Concurrency Strategy
Hibernate Query Language
Hibernate Interceptors and Events
2
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

JDBC Overview
Connection con=null;
try{
//load the driver
Class.forName(OracleDriver);
//create the Connection
con=DriverManger.getConnection(url,un,pw);
con.setAutoCommit(false);
//create the statement
Statement st=con.createStatement();
st.executeQuery(select * from );
con.commit;
}catch{con.rollback();}finally{con.close();}
3
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Features of Hibernate
Hibernate is responsible for establish the
connection, creating statement ,preparing sql
statement required and cleaning the resource.
Hibernate supports two types of connection
1)DriverManager 2)Data Source connection .

Hibernate support two types of transaction


1)JDBC

2)JTA Transaction

Hibernate support various types of mappings .


Hibernate support powerful Object Oriented
Query Language .

4
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Features of Hibernate
Hibernate supports various Caching
Technique .
Default support for batch updates .
Hibernate can run in CME and non
CME Env .
Hibernate supports various types of
primary key generation algorithm .
Hibernate support Lazy loading .
5
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Core objects of hibernate


Configuration(org.hibernate.cfg.Configura
tion)
SessionFactory
(org.hibernate.SessionFactory)
Immutable object
Thread safe
Single instance per database

Session (org.hibernate.Session)
Single unit of work
Not thread safe

Transaction (org.hibernate.Transaction)
6
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Object State
Transient
Object instantiated with new operator and
is not associated with a hibernate session.

Persistent
Has a representation in the database. Can
be a object just has been saved or loaded
from db.

Detached
An object that has been persistent and a
valid reference exists for it outside the
scope of its session.
7
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Association Mappings


Hibernate support following Association
Mapping .
OneToOne
OneToMany
ManyToMany

8
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

One To One Mapping

Employee

Salary_Account

EMP_ID

EMP_NA
ME

SALAR
Y

SAL_ACC_ID

EMP_ID

Sateesh

100000

Harish

150000

9
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

One To Many Mapping

Employee
EMP_ID
1
2

Address

EMP_NA
ME

SALAR
Y

Sateesh

100000

Hrish

150000

AID

STREET

EMP_ID

13th
Phase

14th
cross

2nd cross

10
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Many To Many Mapping


Address

Employee
EMP_ID

EMP_NA
ME

SALAR
Y

Sateesh

100000

Harish

150000

AID

STREET

13th Phase

14th cross

2nd cross

EMP_ADD
EMP_ADD_EMP_I
D

EMP_ADD_AID

3
11
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Connection Management


Hibernate support following Connection
Provider .
DriverManagerConnectionProvider
C3P0 Connection Provider
DataSource Connection Provider
Custom Connection Provider

12
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Connection Management


DriverManager Coonection Provider
By default Hibernate use this connection
provider and this is not good for production .
C3P0 Connection Provider
It is a third party connection pooling algorithm .To
use this we need to add specific property in
hibernate configuration file .
< property name=connection.provider_class>..
C3P0ConnectionProvider </property>
<property name=c3p0.min_size>10</property>
<property name=c3p0.max_size>20</property>
<property name=c3p0.timeout>1</property>
13
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Connection Management


DataSource Coonection Provider
To use this connection provider our hibernate
application must run inside some application
server . Data source connection will
automatically participate in JTA transaction .we
need to add some specific property for this in
hibernate cfg file .
Custom Connection Provider
<property name=connection.provider_class>
CustomConnectionProvider</property>

14
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Transaction Management


Transaction is the process of performing
multiple database operation as a single unit
with all nothing criteria .Transaction must
support ACID property .
A-Atomicity
C-Consistency
I-Isolation
D-Durability

15
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Transaction Management


Hibernate support two types of transaction
1)JDBC
2)JTA Transaction
JDBCTransaction
By default Hibernate use JDBC Transaction internally .To use
JDBC transaction connection must be DriverManager or C3P0 .
JTA Transaction
To use JTA transaction we must use Data source connection .
<property
name="hibernate.connection.datasource">MYSQLDataSourc
e</property>
<property
name=transaction.factory_class>org.hibernate.transaction
.JTATransactionFactory></property>
16
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Transaction Management


<property
name=transaction.manager_lookup_class
>..
JBossTransactionManagerLookup.class</pr
operty>
<property
name=jta.UserTransaction>Jta/UserTrans
action</property>
<property
name=current_session_context_class>jt
a</property>
17
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Cache Implementation


Cache is a presentation of database near
to application .when we implement caching
it will increase the performance .we have
three types of cache scope .
Transactional Scope cache
TX1
Applicatio
n
TX2

Cach
e
Cach
e

DB

18
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Cache Implementation


Process Scope cache

Applicatio
n

TX1
Cache
TX2

DB

19
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Cache Implementation


Clustered Scope cache
JBOSS Instance 1 JVM 1
Applicati
on

TX1
TX2

cach
e

Load
Balancing
Server

DB
Applicati
on

TX1
TX1

cach
e

JBOSS Instance
2

JVM 2

20
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Cache Architecture


First Level Cache
Session Cache is the first level Cache .
Second Level cache or Query Cache
Query cache can be process scope or cluster
scope .
To enable query cache we need to add some
property .
<property
name=cache.use_query_cache>true</property>
<property
name=cache.provider_class>..EhCacheProvider<
/property>
21
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Cache Provider Supported by Hibernate


Cache

Provider
class

Hashtable

Type

Cluster
Safe

Query
Cache

ENV

org.hibernate. Memory No
cache.Hashtab
leCacheProvid
er

Yes

CME
and
nonCME

EHCache

org.hibernate.
cache.EhCach
eProvider

Memory No
,Disk

Yes

CME
and
nonCME

OSCache

org.hibernate.
cache.OSCach
eProvider

Memory No
,Disk

yes

CME
and
nonCME

SwarmCac
he

org.hibernate. clustere Yes


cache.SwarmC d
acheProvider

No

CME
and
nonCME

TreeCache

org.hibernate.
cache.TreeCac
heProvider

yes

CME only

clustere Yes
d
22

Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Cache Concurrency strategies


A concurrency strategy is a mediator which responsible for
storing items of data in the cache and retrieving them from
the cache.
Read-only: A concurrency strategy suitable for data which
never changes.
Nonstrict-read-write: This strategy makes no guarantee
of consistency between the cache and the database.
Read-write: The READ-WRITE strategy works well for data
that changes and must be committed.
Transactional: The TRANSACTIONAL strategy is intended
for use in an environment utilizing the Java Transaction API
(JTA) to manage transactions across a number of XA
resources. This strategy guarantees that a cache remains in
sync with other resources, such as databases and queues .
23
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Cache Concurrency Strategy Support


Cache

read-only

nonstrictread-write

read-write

Hashtable

Yes

Yes

Yes

EHCache

Yes

Yes

Yes

OSCache

Yes

Yes

Yes

SwarmCach
e

Yes

Yes

Treecache

Yes

transactio
nal

Yes

24
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Query Language


Hibernate support various types of Object
Oriented Query Language .
HQL(Hibernate Query Language)
QBC(Query By Criteria)
Native Query

25
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Interceptors and Events


Interceptors
Interceptors provides a call back method from session
to application ,allowling the application to intercept
and manupulate the properties of persistence object
before its saved, updated or loaded .
Two kinds of interceptors
->SessionScope- Applied to pertucular session
Session ses=sf.openSession(new Myinterceptors());
->SessionFactory-Applied to all session created from
session factory .
newConfiguration().setInterceptor(new
Myinterceptors());

26
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Interceptors and Events


Events
If we have to react to particular events in your
persistence
layer, you can also use the Hibernate3 event
architecture.
The event system can be used in addition, or as a
replacement, for interceptors.
All the methods of the Session interface correlate to
an
event.
> LoadEvent,
> FlushEvent
> etc.
27
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Hibernate Interceptors and Events


Event Listener
Implements the appropriate interface for the event it wants to
process and/or extend one of the convenience base classes
public class MyLoadListener implements LoadEventListener
{
// this is the single method defined by the LoadEventListener
interface
public void onLoad(LoadEvent event, LoadEventListener.LoadType
loadType) throws HibernateException {
if ( !MySecurity.isAuthorized( event.getEntityClassName(),
event.getEntityId() ) ) {
throw MySecurityException("Unauthorized access");
}
}
}
28
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Question?

Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.

Anda mungkin juga menyukai