Session03 Hibernate Queries

You might also like

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

ORM

Session 03: Hibernate Queries

Design by: DieuNT1

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


1
Academy - Internal Use
Lecture Topic
01 Queries Intro
02 Native SQL
03 HQL
04 Proxy Object
05 Get and Load Method
06 Persist, Save, Merge, Update,
SaveOrUpdate

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


2
Academy - Internal Use
Lecture 01

QUERIES INTRO

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


3
Academy - Internal Use
Queries Intro
 The Hibernate Query Language (HQL) and Java
Persistence Query Language (JPQL) are both object model
focused query languages similar in nature to SQL.

 JPQL is a heavily-inspired-by subset of HQL.

 A JPQL query is always a valid HQL query, however the


reverse is not true.

 Both HQL and JPQL are non-type-safe ways to perform


query operations. Criteria queries offer a type-safe approach
to querying.

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


Lecture 02

NATIVE SQL

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


5
Academy - Internal Use
Native SQL
 Scalar queries:

 Entity queries:

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


Native SQL
 Name SQL query

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


Lecture 03

HQL

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


8
Academy - Internal Use
Hibernate Query Language (HQL)
 Syntax is quite similar to database SQL language
 Uses class name instead of table name, and property
names instead of column name

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


Hibernate Query Language
 Update a stock name to “DIALOG1″ where stock code is
“7277″

 Delete a stock where stock code is “7277″

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


Lecture 04

PROXY OBJECT

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


11
Academy - Internal Use
Proxy Object
 An object proxy is just a way to avoid retrieving an object
until you need it.

 Is used to substitute an actual entity POJO (Plain Old Java


Object).

 The Proxy class is generated at runtime and it extends the


original entity class.

 Uses Proxy objects for entities is for to allow lazy loading.

 When accessing basic properties on the Proxy, it simply


delegates the call to the original entity.
43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 12
Lecture 05

GET AND LOAD METHOD

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


13
Academy - Internal Use
Get and Load Method
 In hibernate, get() and load() are two methods which is used
to fetch data for the given identifier.
 They both belong to Hibernate session class.
 Get() method return null, If no row is available in the session
cache or the database for the given identifier whereas load()
method throws object not found exception.
// Get Example
User user = (User) session.get(User.class, new Integer(2));

// Load Example
User user = (User) session.load(User.class, new Integer(2));

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


Get and Load Method
 Difference Between get() and load()
Key Get() Load()

Basic It is used to fetch data from the It is also used to fetch data from the
database for the given identifier database for the given identifier

Null Object It object not found for the given It will throw object not found exception
identifier then it will return null object

Lazy or Eager It returns fully initialized object so this It always returns proxy object so this
loading method eager load the object method is lazy load the object

Performance It is slower than load() because it return It is slightly faster.


fully initialized object which impact the
performance of the application

Use Case If you are not sure that object exist then If you are sure that object exist then use
use get() method load() method
43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 15
Lecture 06

PERSIST, SAVE, MERGE, UPDATE,


SAVEORUPDATE

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


16
Academy - Internal Use
Persist, Save, Merge, Update, SaveOrUpdate
 States of Entity Instances
 Transient: this instance is not, and never was, attached
to a Session; this instance has no corresponding rows in
the database; it's usually just a new object that you have
created to save to the database.
 Persistent: this instance is associated with a
unique Session object; upon flushing the Session to the
database, this entity is guaranteed to have a
corresponding consistent record in the database.
 Detached: this instance was once attached to
a Session (in a persistent state), but now it’s not; an
instance enters this state if you evict it from the context,
clear or close the Session, or put the instance through
serialization/deserialization process.
43e-BM/HR/HDCV/FSOFT V1.2 - ©FPT SOFTWARE - Fresher Academy - Internal Use 17
Persist, Save, Merge, Update, SaveOrUpdate
 Persist
The persist method is intended for adding a new entity
instance to the persistence context, i.e. transitioning an
instance from transient to persistent state.

Person person = new Person();


person.setName("John");
session.persist(person);

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


Persist, Save, Merge, Update, SaveOrUpdate
 Save
The save method is an “original” Hibernate method that does
not conform to the JPA specification.
Person person = new Person();
person.setName("John");
Long id = (Long) session.save(person);

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


Persist, Save, Merge, Update, SaveOrUpdate
 Merge
The main intention of the merge method is to update
a persistent entity instance with new field values from
a detached entity instance

Person person = new Person();


person.setName("John");
session.save(person);

session.evict(person);
person.setName("Mary");

Person mergedPerson = (Person) session.merge(person);

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


Persist, Save, Merge, Update, SaveOrUpdate
 Update
As with persist and save, the update method is an “original”
Hibernate method that was present long before
the merge method was added. Its semantics differs in several
key points:
 it acts upon passed object (its return type is void); the update method
transitions the passed object from detached to persistent state;
 this method throws an exception if you pass it a transient entity.

Person person = new Person();


person.setName("John");
session.save(person);
session.evict(person);

person.setName("Mary");
session.update(person);

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


Persist, Save, Merge, Update, SaveOrUpdate
 SaveOrUpdate
This method appears only in the Hibernate API and does not
have its standardized counterpart. Similar to update, it also
may be used for reattaching instances.

Person person = new Person();


person.setName("John");
session.saveOrUpdate(person);

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


Thank you

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


23
23
Academy - Internal Use

You might also like