750 likes | 977 Views
System Design. Object Relational mapping. Introduction. The contribution of technology ORM. The use of technologies of ORM brings many improvements in the development of applications: Simplification of the Code, only "talking object" from the highest to the lowest layer.
E N D
System Design Object Relational mapping
The contribution of technology ORM • The use of technologies of ORM brings many improvements in the development of applications: • Simplification of the Code, only "talking object" from the highest to the lowest layer. • Increase maintainability, there is more code SQL to maintain within the object code. • Increase portability, the database is hidden it is possible to move from one DBMS to another (within the support of ORM selected).
Different ORM Technologies the ORM is a concept and thus different implementations with more or less features will be found. For example • Entity Framework in .Net • Hibernate in Java, Hibernate tutorial • Java Persistence API, Introduction to the Java Persistence API • Ruby has an ORM framework check Toolbox
The process of ORM • Mapping classes. • Mapping attributes. • Mapping relationships. • Inheritance.
1. Mapping classes • In general, classes map to tables in one-to-one mapping. • In some cases, multiple classes can map to a single table, e.g., inheritance. • <<Class model>> <<Data Model>>
2. Mapping attributes. • The most straightforward mapping. • Simple types are common between object paradigm and relational models. • <<Class model>> <<Data Model>>
3. Mapping relationships • Use a Foreign Key Mapping to map a single-valued field. • Use a Foreign Key Mapping to map a collection field.
3. Mapping relationships • Use an Association Table Mapping to map a many-to-many association.
4. Mapping Inheritance • Map the entire class hierarchy to a single table (TPH) • Map each concrete class to its own table (TPC) • Map each class to its own table (TPT)
4. Mapping Inheritance Map the entire class hierarchy to a single table
4. Mapping Inheritance Map each concrete class to its own table
4. Mapping Inheritance Map each concrete class to its own table
Some Guidelines • Every UML Class may become a Table. • Every UML Attribute in Class becomes a Column in a Table. • Every UML Attribute Type becomes a Column Type using a Type Transformation Table. • Every Nullable UML Attribute becomes a Nullable Column, otherwise NOT NULL constraint. • If UML Attribute has Initializer, add DEFAULT value to Column. • For Classes with no generalization and implicit identity, create primary key as sequence id .
Some Guidelines • For Sub-Classes, add the key of each parent class to the Primary Key of the Table and to a Foreign Key constraint. • For Association Classes, add the primary key from each role-playing table to PRIMARY KEY constraint and FOREIGN KEY constraint. • Create PRIMARY KEY for composite aggregations with FOREIGN KEY to aggregating tables and add additional column for PRIMARY KEY. • Create Tables for many-to-many associations with no association classes. • Create PRIMARY KEY, FOREIGN KEY constraints from keys of role-playing tables in many-to-many associations.
Entity Framework • Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. • Its project independent. It can be added to any .Net project, eg. Web application, windows forms ..etc.
Constructing the Entity Framework • Based ON Model [Model First] • Code First
Entity Framework Based On THE Model
Based On The Model • For Inheritance mapping : • Each on its own table • Single table Not support concrete classes mapping
Based on the Model Each on its own table
Based on the Model Single table
Entity Framework Based On Code first
Based On Code First • For Inheritance mapping : • Each on its own table • Single table • concrete classes