ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Introduction To ORM Frameworks

          Ali Shakiba
Procedural Vs. OO
Procedural Vs. OO
• Procedures are core    • Objects are core
  entities                 entities
• Data exist to feed     • Objects consist of
  procedures execution     function and data at the
• Workflow relation        same level
  between procedures     • Rich OO relation:
                            –   Composition
                            –   Inheritance
                            –   Workflow
                            –   …
Object Oriented Concepts

• Class & Object
Object Oriented Concepts, Cont.

• Data (fields, properties, state, attributes)
• Function (methods, procedures)
Object Oriented Concepts, Cont.

• Reference (pointer, link, complex objects
  graph)
Object Oriented Concepts, Cont.

• Encapsulation
  & Interfaces
Object Oriented Concepts, Cont.

• Inheritance & Specialization
Business Logic & Domain Model

• Business Logic: What should the software do?
• Domain Model: OO implementation of BL.
Relational Databases Concepts

•   Table & Tuple
•   Primary Key
•   Foreign Key
•   SQL Query
•   Normalization
•   Transaction
RDB Concepts, Cont.
Object-Relational Impedance Mismatch

• OO does not talk SQL
     • Lots of SQL for lots of objects
     • RDBMs’ dialect
     • Write/change/maintain nightmare
OR Impedance Mismatch, Cont.

• Moving objects between OO and RDB worlds
    • Load on demand
    • Trace and update
    • Identity
OR Impedance Mismatch, Cont.

• RDB does not support OO concepts
  immediately:
     • Inheritance
     • Complex, non-normalized composition
     • Encapsulation and Interface
OR Impedance Mismatch, Cont.

• Who
    • Outlive the other?
    • Is more expensive?
    • Is master/slave?
So Why Still RDB?
• High performance,
  Powerful
• Available, Everywhere
• Proven
  – in theory
  – and in practice
• Known in every language
• Almost no dominate
  replacement
Solution
• RDB+SQL:
           • JDBC (no solution!)
• RDB+ORM:
           • Hibernate (OSS, JBoss, supports EJB3)
           • JDO
           • EJB3
(JDBC/EJB3/JDO JCP Spec, supported by Sun, IBM, Oracle, JBoss and other Java friends.)

• RDB, SQL mapping:
           • iBATIS (IBM Prod.)
• OODB:
           • Db4o (OSS, Recent years tries hard
                       to prove itself! Not dominate yet!)
ORM Features
• Mapping OO concepts to RDB concepts
• Relationship navigation
  – Eager/Lazy loading
• Trace & update changes
  – Lazy update
• Map identities
• Query language, CRUD API
• Transparency
• Transaction
ORM, Mapping OO Concepts to RDB Concepts

• Inheritance
  – Table per Hierarchy
        – Lost Memory
        – No Modularity
  – Table per Class
        – Join, lost time
  – Table per Concrete Class
        – Redundancy
ORM, Mapping OO Concepts to RDB Concepts, Cont.


• Composition
   – Collections (Indexed)
   – Value/Embedded Objects
• Bi-directional reference
ORM, Relationship Navigation

• Lazy Loading
     • Delay loading ‘e’ if it’s rare to load ‘b’ if ‘e’ is loaded
     • Avoid memory waste for unnecessary objects
• Eager Loading
     • Load ‘b’ with ‘e’ if it’s common to load ‘b’ if ‘e’ is loaded
     • Avoid delays for loading objects
ORM, Trace, Update, Identities

• Trace loaded objects
• Preserve relation between objects and
  corresponding RDB data
     • Return same object for request for same data
     • Apply changes to object to same data in RDB
• Knows which object should be update
ORM, Query Language, CRUD API

• Create, Read, Update, Delete Objects
  in/from/to RDB
• Query lang for search for objects stored in
  RDB
• Support SQL, convert SQL result to objects
ORM, Transparency

• Domain model objects should act as if they
  are not persistence object
• Use Repository interfaces and implementation
  to talk to ORM framework
ORM, Transaction

• An API to do atomic jobs
  startTransaction();
  …
  If( everyThingIsOk){
     commit();
  }else{
     rollback();
  }
ORM, Caching

•   Cache loaded objects even if they are not
    needed any more to benefit from request
    for them in near future
ORM Benefits

• Productivity
  – OOP
  – No SQL
• Performance
  – Cache Objects
  – Lazy/Eager Loading
  – Lazy Updating
• Portability
  – Different SQL dialects
When SQL?
• Performance
     • Handwritten SQLs
     • Special SQL queries
     • Special RDBMS features
• Limited SQL
     • Organization policy or DBA
     • Legacy RDBMSs or RDBs
• No OO, i.e. Procedural style
• Create an ORM framework
Case Study: Simple Wiki
Case Study: Simple Wiki, Cont.
Case Study: Simple Wiki, Cont.
Case Study: Simple Wiki, Cont.
Case Study: Simple Wiki, Cont.

More Related Content

Introduction to Object-Relational Mapping

  • 1. Introduction To ORM Frameworks Ali Shakiba
  • 3. Procedural Vs. OO • Procedures are core • Objects are core entities entities • Data exist to feed • Objects consist of procedures execution function and data at the • Workflow relation same level between procedures • Rich OO relation: – Composition – Inheritance – Workflow – …
  • 5. Object Oriented Concepts, Cont. • Data (fields, properties, state, attributes) • Function (methods, procedures)
  • 6. Object Oriented Concepts, Cont. • Reference (pointer, link, complex objects graph)
  • 7. Object Oriented Concepts, Cont. • Encapsulation & Interfaces
  • 8. Object Oriented Concepts, Cont. • Inheritance & Specialization
  • 9. Business Logic & Domain Model • Business Logic: What should the software do? • Domain Model: OO implementation of BL.
  • 10. Relational Databases Concepts • Table & Tuple • Primary Key • Foreign Key • SQL Query • Normalization • Transaction
  • 12. Object-Relational Impedance Mismatch • OO does not talk SQL • Lots of SQL for lots of objects • RDBMs’ dialect • Write/change/maintain nightmare
  • 13. OR Impedance Mismatch, Cont. • Moving objects between OO and RDB worlds • Load on demand • Trace and update • Identity
  • 14. OR Impedance Mismatch, Cont. • RDB does not support OO concepts immediately: • Inheritance • Complex, non-normalized composition • Encapsulation and Interface
  • 15. OR Impedance Mismatch, Cont. • Who • Outlive the other? • Is more expensive? • Is master/slave?
  • 16. So Why Still RDB? • High performance, Powerful • Available, Everywhere • Proven – in theory – and in practice • Known in every language • Almost no dominate replacement
  • 17. Solution • RDB+SQL: • JDBC (no solution!) • RDB+ORM: • Hibernate (OSS, JBoss, supports EJB3) • JDO • EJB3 (JDBC/EJB3/JDO JCP Spec, supported by Sun, IBM, Oracle, JBoss and other Java friends.) • RDB, SQL mapping: • iBATIS (IBM Prod.) • OODB: • Db4o (OSS, Recent years tries hard to prove itself! Not dominate yet!)
  • 18. ORM Features • Mapping OO concepts to RDB concepts • Relationship navigation – Eager/Lazy loading • Trace & update changes – Lazy update • Map identities • Query language, CRUD API • Transparency • Transaction
  • 19. ORM, Mapping OO Concepts to RDB Concepts • Inheritance – Table per Hierarchy – Lost Memory – No Modularity – Table per Class – Join, lost time – Table per Concrete Class – Redundancy
  • 20. ORM, Mapping OO Concepts to RDB Concepts, Cont. • Composition – Collections (Indexed) – Value/Embedded Objects • Bi-directional reference
  • 21. ORM, Relationship Navigation • Lazy Loading • Delay loading ‘e’ if it’s rare to load ‘b’ if ‘e’ is loaded • Avoid memory waste for unnecessary objects • Eager Loading • Load ‘b’ with ‘e’ if it’s common to load ‘b’ if ‘e’ is loaded • Avoid delays for loading objects
  • 22. ORM, Trace, Update, Identities • Trace loaded objects • Preserve relation between objects and corresponding RDB data • Return same object for request for same data • Apply changes to object to same data in RDB • Knows which object should be update
  • 23. ORM, Query Language, CRUD API • Create, Read, Update, Delete Objects in/from/to RDB • Query lang for search for objects stored in RDB • Support SQL, convert SQL result to objects
  • 24. ORM, Transparency • Domain model objects should act as if they are not persistence object • Use Repository interfaces and implementation to talk to ORM framework
  • 25. ORM, Transaction • An API to do atomic jobs startTransaction(); … If( everyThingIsOk){ commit(); }else{ rollback(); }
  • 26. ORM, Caching • Cache loaded objects even if they are not needed any more to benefit from request for them in near future
  • 27. ORM Benefits • Productivity – OOP – No SQL • Performance – Cache Objects – Lazy/Eager Loading – Lazy Updating • Portability – Different SQL dialects
  • 28. When SQL? • Performance • Handwritten SQLs • Special SQL queries • Special RDBMS features • Limited SQL • Organization policy or DBA • Legacy RDBMSs or RDBs • No OO, i.e. Procedural style • Create an ORM framework
  • 30. Case Study: Simple Wiki, Cont.
  • 31. Case Study: Simple Wiki, Cont.
  • 32. Case Study: Simple Wiki, Cont.
  • 33. Case Study: Simple Wiki, Cont.