Object Relational Mapping

Download Report

Transcript Object Relational Mapping

Object Relational Mapping
What does ORM do?
Object Model
ORM
Data Model
Table1
Table2
Table3




Maps Object Model to Relational Model.
Resolve impedance mismatch.
Resolve mapping of scalar and non-scalar.
Database – Independent applications.
How does ORM work?
Table1
Table2
Table3
Mata Data



Meta Data is information about data.
Keeps record of which attribute/s is/are mapped to
which column.
Brain of ORM.
Property to Column





dataOrdered has the same
type, Date.
Three tax attributes
matching one TAX column
and the type is different.
shipTo passes the person
id to <<FK>>.
orderId <<PK>>.
Follows Agile Method
Naming Standard.
Property to Column (cont)
Property
Column
Order.orderID
Order.OrderID
Order.dateOrdered
Order.DateOrdered
Order.getTotalTax()
Order.Tax
Order.shipto.personID
Order.ShipToContactID
OrderItem.ordered
OrderItem.OrderID
OrderItem.item.number
OrderItem.ItemNo
OrderItem.numuberOrdered
OrderItem.NumuberOrdered
Inheritance Mapping





RDMS does not support inheritance!
ORM supports three inheritance mapping;
Map hierarchy to a single table.
Map concrete class to a table.
Map every class to a table.
Map hierarchy to a single table
All attributes are transformed into columns.
Advantages of
Mapping hierarchy to a single table



Very simple method to map objects to a relational
database.
New classes can be added easily by inserting a new
column into the table.
High performance in retrieving the data, because
there are no joins in the query.
Disadvantages of
Mapping hierarchy to a single table



Any changes applied to one class forces the table to
be changed along with the other classes in the same
hierarchy.
Each department has an unused column wasting a lot
of memory space.
Database normalization may be affected.
Map concrete class to a table


Each Concrete class is mapped to its own table.
Both concrete classes inherit the DepID and Name.
Advantages of
mapping concrete class to a table


Data access for a single department is very good.
Ad-hoc reporting for a single department is facilitated
by the fact that each table relates to only one
department.
Disadvantages of
mapping concrete class to a table


Changes in the Department class affects both sales
and purchase departments.
The first disadvantage leads to data redundancy issues.
Map every class to a table


Each class in the Object
Model is mapped to a
corresponding Entity in
the Relational Model.
The Sales and Purchase
entities hold a reference
to the department entity.
Advantages of
mapping every class to a table




Super classes are very easy to modify or create.
The concrete classes are also very easy to modify.
The all structure is very easy to understand thanks to
the one-to-one relationship between classes and
tables.
Polymorphism becomes natural to achieve.
Disadvantages of
mapping every class to a table


Queries are usually slower compare to the previous
solution because sometimes join query may be
necessary.
Ad-hoc reporting can be tough task to achieve, views
resolve this problem.
Hibernate vs iBATIS
Hibernate / iBATIS
Table1
Table2
Table3



They are both open source.
They both use XML to configure Meta Data.
They both require POJO classes format.
Hibernate vs iBATIS (cont)
Advantages Hibernate


SQL statements are automatically generated.
Useful where the object model dictates the rules of
the game.
Disadvantages Hibernate


Complex systems are difficult to implements.
Stored procedure and reporting are difficult to create.
Hibernate vs iBATIS (cont)
Advantages of iBATIS


Useful when the data model already exists or it
dictates the persistence strategy.
Stored procedure and reporting can be achieved very
easily.
Disadvantages of iBATIS


More configuration involved due to the fact that
queries are not automatically generated.
It doesn’t have the vast power of Hibernate like
caching.
Advantages of ORM




Isolation of the database from the rest of the
application, hence the all system is database independent.
Maintenance becomes very easy. Changes to both
models will not affect each other.
Productivity is increased due to the fact the lines of
code are reduced.
Domain objects can be easily mapped to the relational
models.
Advantages of ORM (cont)



ORM is very well established in the computing
industry, facilitating further improvements.
Most of the ORM engines are free in the form of
open source software.
Hybrid system, Hibernate + iBATIS.
Disadvantages of ORM (cont)




Batch processing is not efficient using ORM.
Reporting can not always be performed.
It may be necessary to learn a new query language.
Some ORM engines may not perform stored
procedure.
The End
Any Questions ?