SlideShare a Scribd company logo
UsingUML,Patterns,andJava
Object-OrientedSoftwareEngineering Chapter 10,
Mapping Models to
Relational Schema
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2
Mapping an Object Model to a Database
• UML object models can be mapped to relational
databases:
• Some degradation occurs because all UML constructs
must be mapped to a single relational database
construct - the table
• Mapping of classes, attributes and associations
• Each class is mapped to a table
• Each class attribute is mapped onto a column in the
table
• An instance of a class represents a row in the table
• A many-to-many association is mapped into its own
table
• A one-to-many association is implemented as buried
foreign key
• Methods are not mapped.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3
Mapping a Class to a Table
User
+firstName:String
+login:String
+email:String
id:long firstName:text[25] login:text[8] email:text[32]
User table
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4
Primary and Foreign Keys
• Any set of attributes that could be used to
uniquely identify any data record in a relational
table is called a candidate key
• The actual candidate key that is used in the
application to identify the records is called the
primary key
• The primary key of a table is a set of attributes whose
values uniquely identify the data records in the table
• A foreign key is an attribute (or a set of
attributes) that references the primary key of
another table.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5
Example for Primary and Foreign Keys
User table
Candidate key
login email
“am384” “am384@mail.org”
“js289” “john@mail.de”
firstName
“alice”
“john”
“bd” “bobd@mail.ch”“bob”
Candidate key
Primary key
League table login
“am384”
“bd”
name
“tictactoeNovice”
“tictactoeExpert”
“js289”“chessNovice”
Foreign key referencing User table
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6
Buried Association
LeagueLeagueOwner *1
id:long
LeagueOwner table
...
owner:long
League table
...id:long
• Associations with multiplicity “one” can be implemented
using a foreign key
For one-to-many associations we add the foreign key to the
table representing the class on the “many” end
For all other associations we can select either class at the end
of the association.
owner
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7
Another Example for Buried Association
Transaction
transactionID
Portfolio
portfolioID
...
*
portfolioID ...
Portfolio TableTransaction Table
transactionID portfolioID
Foreign Key
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8
Mapping Many-To-Many Associations
City
cityName
Airport
airportCode
airportName
* *Serves
cityName
Houston
Albany
Munich
Hamburg
City Table
airportCode
IAH
HOU
ALB
MUC
HAM
Airport Table
airportName
Intercontinental
Hobby
Albany County
Munich Airport
Hamburg Airport
cityName
Houston
Houston
Albany
Munich
Hamburg
Serves Table
airportCode
IAH
HOU
ALB
MUC
HAM
In this case we need a separate table for the association
Separate table for
the association “Serves”
Primary KeyPrimary Key
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9
Another Many-to-Many Association
Mapping
PlayerTournament **
id
Tournament table
23
name ...
novice
24 expert
tournament player
TournamentPlayerAssociation
table
23 56
23 79
Player table
id
56
name ...
alice
79 john
We need the Tournament/Player association as a separate table
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10
Realizing Inheritance
• Relational databases do not support inheritance
• Two possibilities to map an inheritance
association to a database schema
• With a separate table (”vertical mapping”)
• The attributes of the superclass and the subclasses
are mapped to different tables
• By duplicating columns (”horizontal mapping”)
• There is no table for the superclass
• Each subclass is mapped to a table containing the
attributes of the subclass and the attributes of the
superclass
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11
Realizing inheritance with a separate table
(Vertical mapping)
User table
id
56
name ...
zoe
79 john
role
LeagueOwner
Player
Player
User
LeagueOwner
maxNumLeagues credits
name
Player table
id
79
credits ...
126
id
LeagueOwner table
56
maxNumLeagues ...
12
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12
Realizing inheritance by duplicating
columns (Horizontal Mapping)
Player
User
LeagueOwner
maxNumLeagues credits
name
id
LeagueOwner table
56
maxNumLeagues ...
12
name
zoe
Player table
id
79
credits ...
126
name
john
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13
Comparison: Separate Tables vs
Duplicated Columns
• The trade-off is between modifiability and
response time
• How likely is a change of the superclass?
• What are the performance requirements for queries?
• Separate table mapping (Vertical mapping)
We can add attributes to the superclass easily by
adding a column to the superclass table
Searching for the attributes of an object requires a join
operation.
• Duplicated columns (Horizontal Mapping)
Modifying the database schema is more complex and
error-prone
Individual objects are not fragmented across a number
of tables, resulting in faster queries
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14
Summary
• Four mapping concepts:
• Model transformation improves the compliance of the
object design model with a design goal
• Forward engineering improves the consistency of the
code with respect to the object design model
• Refactoring improves code readability/modifiability
• Reverse engineering discovers the design from the code.
• Model transformations and forward engineering
techniques:
• Optimizing the class model
• Mapping associations to collections
• Mapping contracts to exceptions
• Mapping class model to storage schemas.

More Related Content

PPT
Ch08lect2 ud
PPT
Ch08lect3 ud
PPT
Ch03lect1 ud
PPT
Ch09lect1 ud
PPT
Ch09lect2 ud
PPT
Ch11lect1 ud
PPT
Ch07lect1 ud
PPT
Ch10lect1 ud
Ch08lect2 ud
Ch08lect3 ud
Ch03lect1 ud
Ch09lect1 ud
Ch09lect2 ud
Ch11lect1 ud
Ch07lect1 ud
Ch10lect1 ud

What's hot (19)

PPT
Ch08lect1 ud
PPT
Ch03lect2 ud
PPT
Ch04lect1 ud
PPT
Ch05lect1 ud
PPT
Ch01lect1 ud
PPT
Ch06lect1 ud
PPT
Ch02lect1 ud
PPTX
Design pattern
PPT
Design Pattern For C# Part 1
PPT
Software Design Patterns
PPT
Software Design Patterns
PPT
Design patterns structuralpatterns(theadapterpattern)
 
PPTX
Software design patterns ppt
PPTX
PDF
Software Design Patterns. Part I :: Structural Patterns
PPTX
Gof design patterns
PPTX
Design patterns
PPTX
PPTX
PATTERNS04 - Structural Design Patterns
Ch08lect1 ud
Ch03lect2 ud
Ch04lect1 ud
Ch05lect1 ud
Ch01lect1 ud
Ch06lect1 ud
Ch02lect1 ud
Design pattern
Design Pattern For C# Part 1
Software Design Patterns
Software Design Patterns
Design patterns structuralpatterns(theadapterpattern)
 
Software design patterns ppt
Software Design Patterns. Part I :: Structural Patterns
Gof design patterns
Design patterns
PATTERNS04 - Structural Design Patterns
Ad

Viewers also liked (15)

PPT
organization charts....rules
PDF
Object oriented software engineering concepts
PPT
Ch11lect2 ud
PPT
ASP.NET System design 2
PPT
Ch05lect2 ud
PPT
Abbott's Textual Analysis : Software Engineering 2
PDF
Object Modeling with UML: Behavioral Modeling
PPTX
Ch9-Software Engineering 9
PDF
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
PPTX
Ch7-Software Engineering 9
PPTX
Ch7 implementation
PDF
final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...
PPT
Texture mapping
PPTX
Object Oriented Software Engineering
PPTX
Ch6 architectural design
organization charts....rules
Object oriented software engineering concepts
Ch11lect2 ud
ASP.NET System design 2
Ch05lect2 ud
Abbott's Textual Analysis : Software Engineering 2
Object Modeling with UML: Behavioral Modeling
Ch9-Software Engineering 9
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Ch7-Software Engineering 9
Ch7 implementation
final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...
Texture mapping
Object Oriented Software Engineering
Ch6 architectural design
Ad

Similar to Ch10lect2 ud (20)

PDF
Mapping objects to_relational_databases
PPT
05 inheritance
PDF
Mapping object to_data_models_with_the_uml
PPT
Mapping inheritance structures_mapping_class
PPT
Best training hibernate_navi_mumbai
PPT
Ch10
PPT
7 data management design
PPT
Database.ppt
PPT
VNSISPL_DBMS_Concepts_ch9
PPT
03 Object Relational Mapping
PPTX
MIT302 Lesson 2_Advanced Database Systems.pptx
PDF
JPA and Hibernate
PDF
Introduction to Database Management Systems: Structure, Applications, and Key...
PPTX
Hibernate
PDF
Reengineering of relational databases to object oriented database
PDF
Reengineering of relational databases to objectoriented
DOC
Hibernate Online Training
PDF
International Journal of Computational Engineering Research(IJCER)
PDF
Hibernate using jpa
PPT
software engineering with jpa and systems
Mapping objects to_relational_databases
05 inheritance
Mapping object to_data_models_with_the_uml
Mapping inheritance structures_mapping_class
Best training hibernate_navi_mumbai
Ch10
7 data management design
Database.ppt
VNSISPL_DBMS_Concepts_ch9
03 Object Relational Mapping
MIT302 Lesson 2_Advanced Database Systems.pptx
JPA and Hibernate
Introduction to Database Management Systems: Structure, Applications, and Key...
Hibernate
Reengineering of relational databases to object oriented database
Reengineering of relational databases to objectoriented
Hibernate Online Training
International Journal of Computational Engineering Research(IJCER)
Hibernate using jpa
software engineering with jpa and systems

Ch10lect2 ud

  • 2. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2 Mapping an Object Model to a Database • UML object models can be mapped to relational databases: • Some degradation occurs because all UML constructs must be mapped to a single relational database construct - the table • Mapping of classes, attributes and associations • Each class is mapped to a table • Each class attribute is mapped onto a column in the table • An instance of a class represents a row in the table • A many-to-many association is mapped into its own table • A one-to-many association is implemented as buried foreign key • Methods are not mapped.
  • 3. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3 Mapping a Class to a Table User +firstName:String +login:String +email:String id:long firstName:text[25] login:text[8] email:text[32] User table
  • 4. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4 Primary and Foreign Keys • Any set of attributes that could be used to uniquely identify any data record in a relational table is called a candidate key • The actual candidate key that is used in the application to identify the records is called the primary key • The primary key of a table is a set of attributes whose values uniquely identify the data records in the table • A foreign key is an attribute (or a set of attributes) that references the primary key of another table.
  • 5. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5 Example for Primary and Foreign Keys User table Candidate key login email “am384” “am384@mail.org” “js289” “john@mail.de” firstName “alice” “john” “bd” “bobd@mail.ch”“bob” Candidate key Primary key League table login “am384” “bd” name “tictactoeNovice” “tictactoeExpert” “js289”“chessNovice” Foreign key referencing User table
  • 6. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6 Buried Association LeagueLeagueOwner *1 id:long LeagueOwner table ... owner:long League table ...id:long • Associations with multiplicity “one” can be implemented using a foreign key For one-to-many associations we add the foreign key to the table representing the class on the “many” end For all other associations we can select either class at the end of the association. owner
  • 7. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7 Another Example for Buried Association Transaction transactionID Portfolio portfolioID ... * portfolioID ... Portfolio TableTransaction Table transactionID portfolioID Foreign Key
  • 8. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8 Mapping Many-To-Many Associations City cityName Airport airportCode airportName * *Serves cityName Houston Albany Munich Hamburg City Table airportCode IAH HOU ALB MUC HAM Airport Table airportName Intercontinental Hobby Albany County Munich Airport Hamburg Airport cityName Houston Houston Albany Munich Hamburg Serves Table airportCode IAH HOU ALB MUC HAM In this case we need a separate table for the association Separate table for the association “Serves” Primary KeyPrimary Key
  • 9. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9 Another Many-to-Many Association Mapping PlayerTournament ** id Tournament table 23 name ... novice 24 expert tournament player TournamentPlayerAssociation table 23 56 23 79 Player table id 56 name ... alice 79 john We need the Tournament/Player association as a separate table
  • 10. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10 Realizing Inheritance • Relational databases do not support inheritance • Two possibilities to map an inheritance association to a database schema • With a separate table (”vertical mapping”) • The attributes of the superclass and the subclasses are mapped to different tables • By duplicating columns (”horizontal mapping”) • There is no table for the superclass • Each subclass is mapped to a table containing the attributes of the subclass and the attributes of the superclass
  • 11. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11 Realizing inheritance with a separate table (Vertical mapping) User table id 56 name ... zoe 79 john role LeagueOwner Player Player User LeagueOwner maxNumLeagues credits name Player table id 79 credits ... 126 id LeagueOwner table 56 maxNumLeagues ... 12
  • 12. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12 Realizing inheritance by duplicating columns (Horizontal Mapping) Player User LeagueOwner maxNumLeagues credits name id LeagueOwner table 56 maxNumLeagues ... 12 name zoe Player table id 79 credits ... 126 name john
  • 13. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13 Comparison: Separate Tables vs Duplicated Columns • The trade-off is between modifiability and response time • How likely is a change of the superclass? • What are the performance requirements for queries? • Separate table mapping (Vertical mapping) We can add attributes to the superclass easily by adding a column to the superclass table Searching for the attributes of an object requires a join operation. • Duplicated columns (Horizontal Mapping) Modifying the database schema is more complex and error-prone Individual objects are not fragmented across a number of tables, resulting in faster queries
  • 14. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14 Summary • Four mapping concepts: • Model transformation improves the compliance of the object design model with a design goal • Forward engineering improves the consistency of the code with respect to the object design model • Refactoring improves code readability/modifiability • Reverse engineering discovers the design from the code. • Model transformations and forward engineering techniques: • Optimizing the class model • Mapping associations to collections • Mapping contracts to exceptions • Mapping class model to storage schemas.

Editor's Notes

  • #5: Litte Primer on Database concepts, a repeat for those of you that have already taken databases.
  • #15: Undisciplined change => degradation of system model