Sie sind auf Seite 1von 27

Object Persistence

12/08/21 1
What is Persistence
 One of the most critical tasks that applications
have to perform is to save and restore data
 In object-oriented systems, there are two kind of
objects:
 Transient objects: exist in memory and are discarded when
an application terminates
 Persistent objects: exist from one execution of an
application 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 the
application is run again
12/08/21 2
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

12/08/21 3
File Systems
Files and record structures
 Fixed length (padded)
 Variable length (delimited)
 Header and detail
 Tagged data (XML)

1234567890123456789012345678901234567890123456789
<Author>
”Simon”,”Bennett”,”Leicester”,”GB”,
1,”Simon”,”Bennett”
Simon Bennett Leicester
<Forename>Simon</Forename> GB 21322012002
2,1,”0077098641”,2002
213,”22-01-2002”
<Surname>Bennett</Surname>
2,2,”0077096738”,2001
</Author>

12/08/21 4
Object Serialization

12/08/21 5
Object Serialization
 Simple persistence method which provides 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 byte
stream suitable for streaming to a file on disk or
over a network
 Persistence by reachability handles complex
objects

12/08/21 6
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 appends
information
 readObject reads data corresponding to
writeObject

12/08/21 7
Java Object Serialization Example
public 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();
12/08/21 8
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 application’s
files, but not in another’s
 Maintenance problems : changes to data
structures mean changes to many programs
 Difficulty combining data: business needs may
mean users want data from different applications

12/08/21 9
DBMS
 Corporate database consolidates data for different
applications
 Each application then has its own view of a subset
of the data

Application 1 Database Application 2

12/08/21 10
DBMS Schema
 Ultimately data in databases is stored in files, but
their structure is hidden from developers

External Schema The view on data used by


application programs.

Conceptual Schema The logical model of data that is


separate from how it is used.

Internal Schema The physical storage of data in


files and indexes.

12/08/21 11
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

12/08/21 12
Types of DBMS
 Relational Database Management System
(RDBMS)
 Object-Oriented Database Management System
(OODBMS)
 Object-Relational Database Management System
(ORDBMS)

12/08/21 13
Relational Databases
 Data organised as tuples in relations (tables)
 Link between data tuples
 primary and foreign keys
 Relational algebra
 project, select, join
 Relational normal forms
 Declarative language
 data definition, consistency, manipulation and querying
 Examples
 Oracle 11g, Microsoft SQL Server, IBM DB2
 PostgreSQL, MySQL, Derby

12/08/21 14
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

12/08/21 15
Object-Oriented Databases

12/08/21 16
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
programming languages 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
12/08/21 17
Advantages of OODBMS
 Complex objects and relationships
 Class hierarchy
 No impedance mismatch
 No need for primary keys
 One data model
 One programming language
 No need for query language
 High performance for certain tasks

12/08/21 21
Disadvantages of OODBMS
 Schema changes
 Lack of agreed standards
 Lack of ad-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 with
complex, irregular data, where data access will follow
predictable patterns (e.g CAD/CAM systems,
manufacturing databases)
12/08/21 22
OODBMS Products
 Versant
 ObjectStore and PSE Pro from eXcelon
 Objectivity/DB
 Intersystems Cache
 POET fastObjects
 DB4O
 Computer Associates Jasmine
 GemStone

12/08/21 23
The Object-Relational Database

12/08/21 24
The Object Relational Model
 The object relational model is an extension of the
relational model, with the following features:
 a field may contain an object with attributes and
operations.
 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 a hybrid DBMS
 has the commercial advantage of being supported
by some of the major RDBMS vendors
 e.g. PostgreSQL, Oracle-X
12/08/21 25
Why RDBMS is still used for objects?

 Many companies already committed to some


RDBMS (huge investment)

 RDBMS is robust and in use for a long time

 ODBMS is yet to include some features

12/08/21 26
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.

12/08/21 27
Object-Relational Mappings

12/08/21 28
OR Mapping Frameworks
 Much effort has been put in recently to making OR
mapping more convenient
 Transparent persistence
 Key features:
 the programmer can work only with objects – no SQL statements in
the code
 selected objects are initially marked as being persistent – thereafter,
changes in those objects are transparently changed in the database, and
the programmer does not have to write code specifically to update the
database
 the framework handles the mapping of the objects to relational
database tables where they are actually stored
 mapping of objects to database tables is usually defined in XML
descriptor files

12/08/21 29
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 or SQL
 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
12/08/21 30

Das könnte Ihnen auch gefallen