Persistence Mechanisms and Architecture Persistence Mechanisms Files hold data Database management systems DBMS hold tables of data relational DBMS or objects object DBMS DBMS us
Trang 1Data Management Design
Trang 2 How to design data management objects
How to extend sequence diagrams to
include data management objects
Trang 4Persistence Mechanisms and Architecture
Persistence Mechanisms
Files hold data
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
Objects can also be serialized directly to files
Trang 5Persistence Design Questions
Can files be used for some storage?
Will the system use an existing DBMS?
Will it use a relational DBMS?
Will it use an object DBMS?
What is the logical layering of the system?
What is the physical layering of the system?
Is the system distributed? Does this include distributed data storage?
What protocols will be used to communicate within the system?
Trang 612345678901234567890123456789012345 Simon Bennett Leicester GB 213
67890123
”Simon”,”Bennett”,”Leicester”,”GB”, 213,”22-01-2002”
Fixed length (padded) – easy to process, but wastes storage
Variable length (delimited) – more difficult to process (looking for
delimiters), but saves storage space
Header and detail – allows for structure in data, may need line type and line no.
Tagged data (XML) – self-describing, but storage overhead for tags.
Trang 7Database Management Systems (DBMS)
Problems with files:
Redundancy (Dư thừa): number of files grows with
applications, and data is duplicated
Inconsistency (Không nhất quán): data is updated in one application’s files, but not in another’s
Maintenance problems (Vấn đề bảo trì) : changes to data structures mean changes to many programs
Difficulty combining data (Khó liên kết dữ liệu): business needs may mean users want data from different
applications
Trang 9The logical model of data that is
separate from how it is used.
The physical storage of data in files and indexes.
Trang 10DBMS Features
Data Definition Language (DDL)
Data Manipulation Language (DML)
Trang 11Advantages & 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
Trang 12 Relational DBMS– represent data in tables
tables consist of rows of data organized in columns
e g Oracle, MySQL, sybase,DB2
Object-relational DBMS – hybrid databases that can store data in tables but can also store objects in tables
e.g PostgreSQL, Oracle-X
Types of DBMS
Trang 13Types of DBMS
Object DBMS– store objects as objects
designed to handle complex nested objects for graphical and multimedia applications
e.g Jusmine, Ontos, ObjectStore
Object Data Management Group (ODMG) standard
Not all object databases conform to the standard
Object databases are closely linked to programming
languages
Some may transparently "materialize" objects
Operations are not stored with classes
Trang 14Using OODBMS for OBJECTS
Advantages
Seamless Transition – no overhead
Object in RAM and Objects in OODBMS behaves same -
no separate mechanisms
Can store any object as it is (e.g picture)
Levels of abstractions - gates to chips
Can store association straight
Then, why still RDBMS is used to store objects ?
Trang 15Why 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
Trang 16Using Relational DBMS for OBJECTS
Most modern business application development projects use object technology and relational databases to store the data.
There is an impedance mismatch between object and
relational technology.
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
"Mapping" will be used to refer to how objects and their relationships
Trang 17Mapping Entity Class
Ánh xạ trực tiếp thành một quan hệ (bảng)
Các thuộc tính đơn giản được ánh xạ thành các cột của bảng
Các thuộc tính tạm thời dùng trong chương trình (thuộc tính suy dẫn) không cần lưu trữ, ngoại trừ để tăng hiệu quả xử lý.
Ví dụ: thuộc averageMark của Student.
address : String Student (StuID, Name, DateOfBirth, Address, PhoneNo)
Khóa đại diện
Trang 18Mapping Collection
EMPLOYEE (EmployeeID , Name, Address, …)
EMPLOYEEPHONE ( EmployeeID , PhoneNumber)
Employee
name : String address : String phoneList : List<String>
…
Trang 19firstName lastName 1
Trang 20Mapping Association 1-1
Khởi tạo khóa chính của một quan hệ vào quan hệ còn lại.
Được gọi là khóa ngoại (Foreign Key)
…
TAIXE (MaTX, TenTX, SoDT, …)
XE (SoDKXe, HieuXe, …, MaTX TAIXE (MaTX, TenTX, SoDT, …, SoDKXe constraint unique)
Trang 21Mapping Association 1-1 dùng Join Table
…
TAIXE (MaTX, TenTX, SoDT, …) XE (SoDKXe, HieuXe, …,)
TAIXE_XE(MaTX constraint unique, SoDKXe constraint unique)
Trang 22name location
…
STAFF (StaffID, Name, Sex…, DepID)
Trang 23Mapping Association 1-n dùng Join table
Order
Customer
has
0 n 1
name address
…
orderedDate shipDate
…
CUSTOMER (CustomerID,
Name, Address, …) ORDER (OrderID, OrderedDate, ShipDate…)
Trang 24Biểu diễn quan hệ n-n
Mối quan hệ n-n được biểu diễn bằng một quan hệ mới có
khóa chính là tổ hợp các khóa chính của 2 quan hệ gốc.
…
startDate daysTimes
…
LECTURE (LectureID, Name, Email, …)
TEACH (LectureID, OfferingID )
Trang 25Biểu diễn quan hệ n-n
STUDENT (studentID, Name, Address, …)
Trang 26Mapping Aggregation
Agregation là quan hệ Assocition đặc biệt:
Quan hệ whole-part giữa 2 đối tượng
Có sự phụ thuộc tồn tại của các đối tượng của một lớp thực thể vào các đối tượng của lớp thực thể khác
Ánh xạ giống như quan hệ Assocition
…
startDate daysTimes
…
Trang 27description price
… 0 n 1
Quan hệ Composition
Thực thể yếu phụ thuộc tồn tại vào thực thể cha.
Khóa chính của quan hệ biểu diễn thực thể cha được dùng làm một bộ phận khóa chính của quan hệ biểu diễn thực thể yếu.
Trang 28Mapping Composition
Record relationship by storing key of one side in the table on the many side
Loan
loanNumber customerNumber amountBorrowed
Payment
date amount 0 n
Trang 29… 0 1
EMPLOYEE (EmployeeID, Name, Sex,…, PartnerID unique)
EmployeeID Name Sex … PartnerID
Trang 30Quan hệ đệ quy 1-n
CUSTOMER (CustomerID, Name, …, IntroducerID)
CustomerID Name … IntroducerID
introduced
person
Trang 31Biểu diễn quan hệ đệ quy n-n
PART
contain
0 n
description madeIn
… 0 n
quantity
Trang 32Quan hệ Generalization - Specialization
Có 3 chiến lược cài đặt quan hệ tổng quát hóa – Đặc biệt hóa:
1. Chỉ cài đặt một bảng cho lớp cha bao gồm tất cả các thuộc
tính của lớp con.
2. Cài đặt các bảng cho cả lớp cha và các lớp con và chia sẻ
chung một khóa chính.
3. Chỉ cài đặt các bảng cho các lớp con, lập lại các thuộc tính
của lớp cha trong từng bảng.
Trang 33Quan hệ Generalization - Specialization
tenSV gioitinh diachi
Trang 34Quan hệ Tổng quát hóa – Đặc biệt hóa
name address hiredDate
SALARY
billingRate
CONSULTANT
Trang 35 Giải pháp 1:
EMPLOYEE (EmployeeID, Name, Address, HiredDate, EmployeeType,
HourLyRate, AnnalSalary, StockOption, ContractNumber, BillingRate)
Giải pháp 2:
EMPLOYEE (EmployeeID, Name, Address, HiredDate)
HOURLYEMPLOYEE (EmployeeID, HourlyRate)
SALARYEMPLOYEE (EmployeeID , AnnalSalary, StockOption)
CONSULTANT (EmployeeID, ContractNumber, BillingRate)
Trang 36Các quan hệ đặc biệt khác
Đối xử như quan hệ 1:N
PLAYER (playerID, name, …, teamID)
TEAM (teamID, name, coachName, …)
…
name coachName
…
Trang 37Các quan hệ đặc biệt khác
Đối xử như quan hệ N:N
LECTURE (LectureID, Name, Email…)
OFFERING (OfferingID, StartDate, DayTimes …)
TEACH (LectureID, OfferingID)
Đối xử như quan hệ 1:N
LECTURE (LectureID, Name, Emailm, …)
…
startDate daysTimes
…
Trang 38Object DBMS
ODBMS have the advantage that objects can be stored directly
Object Data Management Group (ODMG) standard
Not all object databases conform to the standard
Object databases are closely linked to programming
languages with ways of navigating through the database
Trang 40Designing Data Management Classes
Alternatives (two in bold are covered here):
add save and retrieve operations to classes
make save and retrieve class-scope methods
allow all persistent objects to inherit from a PersistentObject superclass
use collection classes to manage persistence
use broker classes to manage persistence
use a parameterized class (generic) to handle persistence for different classes
Trang 42Using a Framework
O-R Mapping: Hibernate
J2EE Application Servers
Enterprise JavaBeans (EJBs) can be used – Entity Beans for business objects
Container-Managed Persistence (CMP) is a framework for J2EE containers to handle the persistence of instances of entity beans
Use J2EE Patterns (Alur et al., 2001)