Upload
hop-huynh
View
227
Download
0
Embed Size (px)
Citation preview
8/8/2019 1 Object Persistence
1/30
1/18/2011 11
Object Persistence
8/8/2019 1 Object Persistence
2/30
21/18/2011 2
What is Persistence
One of the most critical tasks that applicationshave to perform is to save and restore data
In object-oriented systems, there are two kind ofobjects:
Transient objects: exist in memory and are discarded whenan application terminates
Persistent objects: exist from one execution of anapplication to another or be shared among different
instances of applications. Persistence is the storage of data from working
memory so that it can be restored when theapplication is run again
8/8/2019 1 Object Persistence
3/30
31/18/2011 3
Persistence Mechanisms
There are several object persistence mechanisms:
Files hold data typically on magnetic media: disks
and tapes
Objects can also be serialized directly to files
Database management systems (DBMS) hold
tables of data (relational DBMS) or objects (object
DBMS)
DBMS use files to store data or objects, but they hide the
physical processes for storing data beneath a layer of
abstraction
8/8/2019 1 Object Persistence
4/30
41/18/2011
1234567890123456789012345678901234567890123456789
Simon Bennett Leicester GB 21322012002
Simon,Bennett,Leicester,GB,
213,22-01-2002
1,Simon,Bennett
2,1,0077098641,20022,2,0077096738,2001
Simon
Bennett
4
File Systems
Files and record structures
Fixed length (padded)
Variable length (delimited)
Header and detail Tagged data (XML)
8/8/2019 1 Object Persistence
5/30
51/18/2011
Object Serialization
8/8/2019 1 Object Persistence
6/30
61/18/2011
Object Serialization
Simple persistence method whichprovides a
program the ability to read or write a whole object
to and from a stream of bytes
Allows Java objects to be encoded into a bytestream suitable for streaming to a file on disk or
over a network
Persistence by reachability handles complex
objects
8/8/2019 1 Object Persistence
7/30
71/18/2011
Java Object Serialisation
A serialisable class must do the following
implement the java.io.Serializable interface
identify the fields that should be serialisable
non-transient and non-static fields are serialised by default
use the serialPersistentField member or the transient keyword
Optionally, the class can define the following
methods
writeObject controls saved data or appendsinformation
readObject reads data corresponding to
writeObject
8/8/2019 1 Object Persistence
8/30
81/18/2011
Java Object Serialization Example
pu blic class Address implements Serializable {
// Class Definition
}
// Serialise an object
FileOutputStream f = new FileOutputStream("tmp");ObjectOutput out = new ObjectOutputStream(f);
out.writeObject(new Address());
out.flush();
out.close();
// Deserialise an object
FileInputStream f = new FileInputStream("tmp");
ObjectInput in = new ObjectInputStream(f);
Address address = (Address) in.readObject();
in.close();
8/8/2019 1 Object Persistence
9/30
91/18/2011 9
Database Management Systems (DBMS)
Problems with files and serialization:
Redundancy: number of files grows with
applications, and data is duplicated
Inconsistency: data is updated in one applicationsfiles, but not in anothers
Maintenance problems : changes to data
structures mean changes to many programs
Difficulty combining data: business needs may
mean users want data from different applications
8/8/2019 1 Object Persistence
10/30
101/18/2011 10
DBMS
Corporate database consolidates data for different
applications
Eachapplication then has its own view ofa subset
of the data
DatabaseApplication 1 Application 2
8/8/2019 1 Object Persistence
11/30
111/18/2011 11
DBMS Schema
Ultimately data in databases is stored in files, but
their structure is hidden from developers
Conceptual Schema
External Schema
Internal Schema
The view on data used by
application programs.
The logical model of data that isseparate from how it is used.
The physical storage of data in
files and indexes.
8/8/2019 1 Object Persistence
12/30
121/18/2011 12
Advantages & Disadvantages of DBMS
Advantages
Eliminate unnecessary duplication of data
Enforce data integrity through constraints
Changes to conceptual schema need not affect external schema
Changes to internal schema need not affect the conceptual schema Many tools are available to manage the database
Disadvantages
Cost of investing in the DBMS
Running cost, including staff (Database Administrators) to manage the
DBMS
Processing overhead in converting data to format required by programs
8/8/2019 1 Object Persistence
13/30
131/18/2011
Types of DBMS
Relational Database Management System
(RDBMS)
Object-Oriented Database Management System
(OODBMS) Object-Relational Database Management System
(ORDBMS)
8/8/2019 1 Object Persistence
14/30
141/18/2011
Relational Databases
Data organised as tuples in relations (tables)
Link between data tuples
primary and foreign keys
Relationalalgebra
project, select, join
Relational normal forms
Declarative language
data definition, consistency, manipulation and querying
Examples
Oracle 11g, Microsoft SQLServer, IBMDB2
PostgreSQL, MySQL, Derby
8/8/2019 1 Object Persistence
15/30
151/18/2011
Object-Relational Impedance Mismatch
Object-oriented application development and relational
data management results in clash of two incompatible
models
Code to map between models is considerable overhead,
costly and hard to maintain
8/8/2019 1 Object Persistence
16/30
161/18/2011
Object-Oriented Databases
8/8/2019 1 Object Persistence
17/30
171/18/2011
Object-Oriented Databases
Object store objects as objects
Seamless Transition no overhead
Designed to handle complex nested objects for graphical
and multimedia applications, e.g Jusmine, Ontos,
ObjectStore
Object databases are closely linked to
programminglanguages with ways of navigating
through the database
Some will transparently 'materialize' objects from the
database when they are referred to
Operations are not stored with classes
8/8/2019 1 Object Persistence
18/30
181/18/2011
Object Identifiers
RDBMS entities are uniquely identified by primary keys
relationships are represented by matching primary key-foreign key data
Identification depends on the values in the key fields
Object-oriented database
stores object identifiers (OIDs) within an object to indicateother objects to which it is related.
The object identifier is not visible to the user or databaseprogrammer
An object remains the same object even when its statetakes on completely new values
8/8/2019 1 Object Persistence
19/30
191/18/2011
Object identity and equality
every object has unique and immutable objectidentifier (OID)
sharing of objects through references
two objects are identical if they have the same
OID two objects are equal if they have the same state
Equality can exist at different levels: Shallow equality: same state values (e.g. two
CustomerOrder objects have same values) Deep equality: same state values and related objects also
contain same state values (e.g. two CustomerOrder objectshave same values and all their related OrderItem objectshave same values)
8/8/2019 1 Object Persistence
20/30
201/18/2011
Objects and Literals
Objects can change their state values, and are
described as being mutable
Another component of the object data model is the
litera
l, which represents avalue or set ofvalueswhich cannot be changed
A literal is described as being immutable. Literals
do not have OIDs.
8/8/2019 1 Object Persistence
21/30
211/18/2011
Advantages of OODBMS
Complex objects and relationships
Class hierarchy
No impedance mismatch
No need for primary keys
One data model
One programminglanguage
No need for query language
Highperformance for certain tasks
8/8/2019 1 Object Persistence
22/30
221/18/2011
Disadvantages of OODBMS
Schema changes
Lack ofagreed standards
Lack ofad-hoc querying
In general,
RDBMSs are probably more suitable for databases with a
variety of query and user interface requirements (i.e. most
mainstream business applications),
while OODBMSs are appropriate for applications withcomplex, irregular data, where data access will follow
predictable patterns (e.g CAD/CAM systems,
manufacturing databases)
8/8/2019 1 Object Persistence
23/30
231/18/2011
OODBMS Products
Versant
ObjectStore and PSE Pro from eXcelon
Objectivity/DB
Intersystems Cache
POET fastObjects
DB4O
Computer Associates Jasmine
GemStone
8/8/2019 1 Object Persistence
24/30
241/18/2011
The Object-Relational Database
8/8/2019 1 Object Persistence
25/30
251/18/2011
The Object Relational Model
The object relational model is an extension of therelational model, with the following features:
a field may contain an object with attributes andoperations.
complex objects can be stored in relational tables the object relational model offers some of the advantages
of both the relational and object data models
An object relational DBMS is sometimes referred
to as ahybrid
DBMS has the commercialadvantage of being supported
by some of the major RDBMS vendors
e.g. PostgreSQL, Oracle-X
8/8/2019 1 Object Persistence
26/30
261/18/2011 26
Many companies already committed to some
RDBMS (huge investment)
RDBMS is robust and in use for along time
ODBMS is yet to include some features
Why RDBMS is still used for objects?
8/8/2019 1 Object Persistence
27/30
271/18/2011 27
Using Relational DBMS for OBJECTS
Most modern business application development projects
use object technology and relational databases to store the
data.
To store objects in a relational database, the objects have
to be flattened into tables Complex objects have to be taken apart and the parts stored in different
tables
When retrieved from the database, the object has to be reassembled
from the parts in different tables
O-R Mapping (Object Relation Mapping)
"Mapping" will be used to refer to how objects and their relationships
are mapped to the tables in a relational database.
8/8/2019 1 Object Persistence
28/30
281/18/2011
Object-Relational Mappings
8/8/2019 1 Object Persistence
29/30
291/18/2011
OR Mapping Frameworks
Much effort has been put in recently to making ORmapping more convenient
Transparent persistence
Key features:
the programmer can workonly with objects no SQL statements inthe code
selected objects are initially marked as being persistent thereafter,changes in those objects are transparently changed in the database, andthe programmer does not have to write code specifically to update thedatabase
the frameworkhandles the mapping of the objects to relationaldatabase tables where they are actually stored
mapping of objects to database tables is usually defined in XMLdescriptor files
8/8/2019 1 Object Persistence
30/30
301/18/2011 30
OR Mapping Frameworks
Hibernate
maps Java types to SQL types
transparent persistence for classes meeting certain requirements
generates SQL for more than 25 dialects behind the scenes
provides data query and retrieval using either HQL orSQL can be used stand-alone with Java SE or in Java EE applications
Java Persistence API (JPA)
Enterprise Java Beans Standard 3.0
introduced annotations to define mapping javax.persistence package
Toplink
JDO (Java Data Object)
OBJ