Вы находитесь на странице: 1из 12

Implementing Data Persistence Using the Hibernate Framework

Objectives

In this session, you will learn to:


Work with persistent objects
Implement transactions

Ver. 1.0 Slide 1 of 12


Implementing Data Persistence Using the Hibernate Framework

Updating Objects

To update the already persistent objects, you need to:


Load the objects in the persistence context by using the get()
or the load() method.
Store the objects back into the database after performing the
required modifications on them.
To update any modified object into the database table, you
can use the update() or the saveOrUpdate() method.

Ver. 1.0 Slide 2 of 12


Implementing Data Persistence Using the Hibernate Framework

Updating Objects (Contd.)

For example:
..............
..............
transaction.begin();
Employee
empUpdate=(Employee)session.load(Employee.class,
objID);
empUpdate.setAddress("Hudson Street, California");
session.update(empUpdate);
transaction.commit();

Updates
Assigns the empUpdate object
the empUpdate object ainnew
the database
employee by using the
address.
update() method.

Ver. 1.0 Slide 3 of 12


Implementing Data Persistence Using the Hibernate Framework

Deleting Objects

You can delete an already persistent object by using any


one of the following methods:
delete()
remove()
The process of deleting an object is similar to the process of
modifying a persistent object.
To delete a persistent object, you need to:
Load it into the persistence context by invoking either of the
load() or the get() method on the Session object.
Delete it by invoking the delete() or the remove() method
on the Session object when the object is in the persistence
context.

Ver. 1.0 Slide 4 of 12


Implementing Data Persistence Using the Hibernate Framework

Deleting Objects (Contd.)

For example:
..............
..............
transaction.begin();
Employee
empUpdate=(Employee)session.load(Employee.class,
objID);
session.delete(empUpdate);
transaction.commit();

Calls the delete()


Retrieves method
the data into on the session
the empUpdate object object to delete
from the the
database
retrieved empUpdate
by using the object.
load() method.

Ver. 1.0 Slide 5 of 12


Implementing Data Persistence Using the Hibernate Framework

Just a minute

The second parameter of the get() method accepts a


____________ reference, which is an identifier for the
object to be retrieved.

Solution:
Serializable

Ver. 1.0 Slide 6 of 12


Implementing Data Persistence Using the Hibernate Framework

Demo: Working with Persistent Objects

Problem Statement:
The Grant University website contains a registration page that
allows the students to register themselves on the website. The
registration page consists of fields that accept data from the
students. When a students clicks the submit button on this
page after entering the required data, the details provided by
the students should be stored in the database. In addition, a
success page should be displayed showing a welcome
message and the user ID of the student.
You have been assigned the task to implement the preceding
functionality in the Grant University website. How will you
accomplish this task?

Ver. 1.0 Slide 7 of 12


Implementing Data Persistence Using the Hibernate Framework

Demo: Working with Persistent Objects (Contd.)

Solution:
To complete the preceding assignment, you need to perform
the following tasks:
1. Create a class for saving the student data.
2. Modify the RegistrationForm.java file.
3. Modify the Registration.xhtml file.
4. Execute the application.

Ver. 1.0 Slide 8 of 12


Implementing Data Persistence Using the Hibernate Framework

Implementing Transactions

A transaction can be defined as a sequence of operations.


If a transaction is successful, all the data modifications
performed in the database will be committed and saved.
If a transaction at any step fails or an error occurs, the
whole transaction is rolled back to undo the data
modifications done in the database.
Transactions allow developers to combine database
operations into one logical unit and ensure that they are
performed together.

Ver. 1.0 Slide 9 of 12


Implementing Data Persistence Using the Hibernate Framework

Properties of a Transaction

A transaction is a sequence of operations performed


together as a single logical unit of work.
A single unit of work must possess the following properties
called the ACID properties:
Atomicity
Consistency
Isolation
Durability

States that any


data
all data
change
eithermodifications
allisthe
in in
adata
consistent
datamodifications
made
by a completed
by
state
concurrent
after
are transaction
a transaction
transactions
performed is
or none
remains
are
completed
isolated
of them permanent
aresuccessfully.
from theinmodifications
performed. the system. made by other concurrent
transactions.

Ver. 1.0 Slide 10 of 12


Implementing Data Persistence Using the Hibernate Framework

Summary

In this session, you learned that:


To update the already persistent objects, you need to first load
the objects in the persistence context by using the get() or
the load() method.
To update the modified object into the database table, you can
use the update() or the saveOrUpdate() method.
You can delete an already persistent object by using the
delete() or the remove() method.
A transaction can be defined as a sequence of operations
performed together as a single logical unit of work.
If a transaction is successful, all the data modifications
performed in the database will be committed and saved.
However, if a transaction at any step fails or an error occurs,
the whole transaction is rolled back to undo the data
modifications done in the database.

Ver. 1.0 Slide 11 of 12


Implementing Data Persistence Using the Hibernate Framework

Summary (Contd.)

Transactions allow developers to combine database


operations into one logical unit and ensure that all these
operations are performed together.
A single unit of work must possess the following properties
called the ACID properties:
Atomicity
Consistency
Isolation
Durability

Ver. 1.0 Slide 12 of 12

Вам также может понравиться