0% found this document useful (0 votes)
9 views5 pages

Hibernate Document

Uploaded by

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

Hibernate Document

Uploaded by

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

What is Hibernate?

Hibernate is an ORM (object Relational Mapping) Framework.


Object means Classes
Relational mean Collection of Tables
ORM means Relation between Classes and Tables.

Advantages of ORM:
Reduce the repeatable code.
I t provides portability, we can shift 1DBto another DB easily.
Less development time.
Maintainability easy, because less code.
Hibernate uses Cache Mechanism
In Hibernate we can write HQL (Hibernate Query Language)
Hibernate using Lazy loading to improve the performance
Hibernate using Batch Updates to improve the performance
Create tables automatically
Generates Key Automatically

Hibernate Life Cycle:

**
savel)
evictu
******************s ersist()
session.closel)
new
saveOrUpdate)
*************
Session

Transient Persistent Detached

lock
deletel) update
mergel)
saveOrUpdate
*******************
Beu
load)
HaL query
* * * * * * * * * * * * * * * * * *

Hibernate Connection:

Configuration cfg=new AnnotationContigunation().configuse("hibernate.cfg.xml")


1/load the configuration file
SessionFactory sf=cfg.buildsessionfactory();
1/ read the configuration properties
/1 and open the Session.
Session s=sf.openSession();
5.save() or
s.update(); etc-, hibernate operations
Transaction tx
=s.beginTransaction();
tx.commit();// commit the data into database
s.close()ll close the sesssion

What are Hibernate Core Interfaces?

Configuration
Transaction
Session Factory
Session
Query or Criteria

Differences between savel) and persist()?


Savel)
Return type is Serialiable
I t returns ID
I t will work outside the transaction boundary also
It supports Only Hibernate
I t will take more time to execute

Persist():
Return type is void
>It doesn't return any ID, because return type is void.
I t will work within the boundary only
I t supports both Hibernate and JPA
I t will take less time to execute

Differences Between save() and SaveOrUpdate()0?

Savel):
I t l create always new record in Database.
I t brings Object Transient state to Persistent State

SaveOrUpdatel):
i f already record is available with ID it 'l update the record.
I f record is not available with 1D, it inserts new record into DB.
I t brings Object Transient To persistent To detached state.

Evict():
Evict() method can remove single objectfrom the session
Clear():
Clear() method will remove entire session Object

Closel):
Close the session by calling Session.close() method
Differences betweenload()and get()?
Load():
If Object is not available with given id it throws
ObjectNotFoundException
It always return Proxy(Dummy) Object, so this method is Lazy loading
i t is slightly faster than get method
i f you are sure that ID object is exist you can use load()
Example: songs, or movie trailer

get():

if Object is not available with given id it returns NULL


i t always fetch the data from DB, there is no proxy Object In get method,
so, it is Eager loading.
I t is slowerthan load(), because it returns fully initialized Object, it will
impact on Application of the Performance.
I f you are not sure with ld exist or not, then you can use get()
Example: stocks, cricket live score, election results

What is Cache?
I f you want to fetch the records from the database,
If you fire same request multiple times it has to fetch the data from
database again and again.
I t needs to travel the request from Client-> Application -> Database
So, itwillimpact on application performance.
When we fired same request again and again, instead of fetching the
data from database again and again, we can maintain Cache to store the
data.
For the first time it will fetch the data from database and it 1l stores in
Cache memory.
While fetching the same data for the 2r time onwards it 1l fetch the
data from the Cache, the request no needs to travel till Database.

First Level Cache:

First Level Cache by default Enabled


For every session request, it ll create Objects in Cache
>First Level Cache is available only session who maintains it.
First Level Cache is maintained at the Session level
Session is light weight Object, Because Session Factory will load
configuration file and get the session Object.
First level cache is short lived Object, because it resides in RAM
Session is associated with Connection Object.

Second Level Cache:


Second Level Cache we have to Enabled, it is not enabled by default.
Second Level Cache is Global for all requests
Second Level Cache is maintained at the Session Factory level.
Session Factory Heavy Weight Object, Because Session Factory wilload
the configuration file and provide the session Object
Second Level Cache is long lived Object, because it resides in Hard disk.
Second Level Cache at the time of Application destroyed.

Note
W e can enable Second Level Cache with EHCache or OSCache or
WarmCache or JBossCache

What is Connection pooling?


Connection pooling is Opening a connection to a Database is generally
much more expensive, than executing a SQL statement.
A connection pool s used to minimize the number of Connections
opened between application and database.
It serves as librarian, checking out connections to application code as
need.

You might also like