SlideShare a Scribd company logo
Object/Relational Persistence and ORM Persistent data in modern object-oriented applications & efficient data management
What is persistence? “Persistence” where data (state) outlives the process that created it In object-oriented applications: objects in a system are by default transient some of the objects in the graph are made persistent this subgraph of persistent instances can later be re-created in the same process or another process We need a data model and data store for persistent data... The Relational Data Model and SQL-based Database Management Systems.
What is persistence? PERSISTENCE APPROACHES 1. A well-know and widely used DAO design pattern to wide complex  JDBC code and no portable SQL from business logic. ORM also uses DAO pattern 2.  IBatis is a best persistence layer for legacy systems, Batch processing system and Report generation. JDBC, Spring-JDBC and IBatis(SQL Maps)
What is persistence? PERSISTENCE APPROACHES 3.  EJB entity beans (CMP) Not fully object-oriented Doesn’t support polymorphic associations and queries Not portable across application servers Not fertilizable. Need to use DTO(Data transfer object or Value  objects), and additional load Persistence runs only with EJB container Problems for Test  Cases) In EJB3 no CMP, uses ORM for persistence Hibernate is good for JBOSS and major app. Servers Top Link is good for Oracle app. server
What is persistence? PERSISTENCE APPROACHES 4. Object-oriented database system (ODBMS) JDO (Java Data Objects) are popular. But, object-databases are not widely adopted. 5.  XML Parses Advantages over Serialization but it is just another text file format.
What is persistence? PERSISTENCE APPROACHE 6. THE BEST FOR THE MOST PROBLEMS ORM (Object-Relation Model) An API for performing basic CRUD operations on objects of persistent classes A language or API for specifying queries that refer to classes and properties of classes A facility for specifying mapping metadata A technique for the ORM implementation to interact with transactional objects to perform dirty checking, lazy association fetching and other optimization functions.
What is persistence? PERSISTENCE APPROACHE 6. THE BEST FOR THE MOST PROBLEMS ORM (Object-Relation Model) ORM advantages: Productivity  -  no tedious SQL code Maintainability  - Fewer lines of code makes the system more understandable Performance Vendor Independence
The persistence layer How does the Java application interact with the SQL database? Application layers group related code  by concern prevent leakage of concerns have clear interfaces to avoid inter-layer dependencies A typical  Layered Architecture  and its interface dependencies: Persistence Layer Business Layer Presentation Layer Utility and Helper Classes
Tabular Data vs. Domain Model Some applications work exclusively with a tabular representation of persistent data: application works with tabular result sets in all layers straightforward with JDBC and SQL even better with detached RowSet (JSR 114) hard to code re-usable business logic Other applications have an object-oriented  domain model : significant business logic, benefits from object-oriented techniques such as inheritance, polymorphism, and patterns greater degree of abstraction of the underlying relational data SQL / JDBC no longer so appropriate certain operations (batch processing, reporting) are more tabular in nature Some applications benefit from  both , in different places.
Domain Model and the paradigm mismatch Classes implement the business entities of our domain model attributes of the entity are properties of our Java class associations between entities are also implemented with properties Let’s see if there is a problem mapping this to tables and columns... 1 1..* User userName: String address: String billingDetails: Set BillingDetails accountNumber: String accountName: String accountType: String user: User
Creating tables for the Domain Model SQL schema design for trivial cases is ... trivial: We’ll see the 5 problems of the O/R paradigm mismatch appear as we gradually make our model more complex… create table USER ( USER_NAME  varchar not null primary key, ADDRESS  varchar not null) create table BILLING_DETAILS ( ACCOUNT_NUMBER varchar not null primary key, ACCOUNT_NAME  varchar not null, ACCOUNT_TYPE  varchar not null, USER_NAME  varchar foreign key references USER)
The problem of granularity should we create a new  ADDRESS  table? should we create a new SQL data type and change the column? user-defined data types (UDT) are not portable and the standard is weak We usually add new columns to  USER  with built-in SQL data types: create table USER ( USER_NAME  varchar not null primary key, ADDRESS_STREET  varchar not null, ADDRESS_CITY  varchar not null, ADDRESS_ZIPCODE  varchar not null ) User userName: String billingDetails: Set Address street: String city: String zipcode: String
The problem of subtypes We create subclasses of  BillingDetails : and use polymorphism in Java to implement our billing strategy. How do we represent subtypes in our relational model? 1 1..* User BillingDetails CreditCard Cheque
The problem of identity In Java, we have two notions of "sameness" object identity is the memory location of an object,  a==b object equality (what is this really?),  a.equals(b) In SQL databases, we identify a particular row using the primary key and the table name. Often, this is a (meaningless)  surrogate key ! What is the relationship between the three different types of identity in our domain model? Is the surrogate key propagated into the domain model?
The problem of associations Object-oriented languages represent entity relationships as object references  (pointers) and  collections  of object references Relational databases represent entity relationships as copies of primary key values referential integrity ensured by  foreign key  constraints The mismatch: object references are directional, there is no such concept in the relational model many-to-many associations require a  link table  in relational databases
The problem of object graph navigation In Java, we "walk" the object graph by following references: david.getBillingDetails().getAccountName() In SQL, we join tables to get the required data: select * from USER u left outer join BILLING_DETAILS bd on bd.USER_ID = u.USER_ID where u.USERNAME = “david" Avoid the n+1 selects problem by minimizing the SQL required for graph walking!
The cost of the mismatch There problems can, at least theoretically, be solved using handwritten SQL/JDBC by writing a lot of tedious code (maybe 30% of your codebase) The “mismatch problem” is real better UDT support in SQL will not solve all the issues not all applications are suitable for table-oriented approaches Is the solution design patterns (DAO) or programming models (EJB entity beans)? "How should we implement the persistence layer in our application?"
Object/Relational Mapping Object / Relational Mapping (ORM) solve the mismatch problem in middleware an ORM solution transforms data from the object-oriented representation to the relational representation metadata governs this transformation Elements of an ORM implementation a programming model for the domain objects an API for performing CRUD operations a query language or other query facility a metadata facility

More Related Content

PPTX
CA ERwin Data Modeler End User Presentation
PPTX
Object oriented database model
PPT
Introductin to Data Modeling.
PPT
OMD chapter 2 Class modelling
PDF
Object oriented software engineering concepts
PPT
Uml Omg Fundamental Certification 3
PPTX
Introduction of C# & MVC
CA ERwin Data Modeler End User Presentation
Object oriented database model
Introductin to Data Modeling.
OMD chapter 2 Class modelling
Object oriented software engineering concepts
Uml Omg Fundamental Certification 3
Introduction of C# & MVC

What's hot (20)

PPT
RDBMS_Unit 01
PPTX
DBMS - ER Model
PPT
Overview of Object-Oriented Concepts Characteristics by vikas jagtap
PPT
Ch 12 O O D B Dvlpt
PDF
Hibernate II
PPTX
PPTX
Fundamentals of Data Modeling and Database Design by Dr. Kamal Gulati
PPT
Datastage database design and data modeling ppt 4
PPT
Module 5 oodb systems semantic db systems
PPTX
Logical database design and the relational model(database)
PPT
Intro Uml
PDF
Chapter 3 Entity Relationship Model
PDF
Summary data modelling
PPTX
Object database standards, languages and design
PPTX
Object oriented database concepts
PPT
OODM-object oriented data model
PPTX
Chapter 7 relation database language
PDF
George McGeachie's Favourite PowerDesigner features
PPTX
Data Retrival
RDBMS_Unit 01
DBMS - ER Model
Overview of Object-Oriented Concepts Characteristics by vikas jagtap
Ch 12 O O D B Dvlpt
Hibernate II
Fundamentals of Data Modeling and Database Design by Dr. Kamal Gulati
Datastage database design and data modeling ppt 4
Module 5 oodb systems semantic db systems
Logical database design and the relational model(database)
Intro Uml
Chapter 3 Entity Relationship Model
Summary data modelling
Object database standards, languages and design
Object oriented database concepts
OODM-object oriented data model
Chapter 7 relation database language
George McGeachie's Favourite PowerDesigner features
Data Retrival
Ad

Similar to 01 Persistence And Orm (20)

PPT
fdocuments.in_introduction-to-ibatis.ppt
PPTX
Building nTier Applications with Entity Framework Services (Part 1)
DOCX
Object relationship mapping and hibernate
PPTX
Building nTier Applications with Entity Framework Services (Part 1)
PPTX
Object relational database management system
PPT
Dev381.Pp
PPTX
PATTERNS07 - Data Representation in C#
PDF
Entity Framework Interview Questions PDF By ScholarHat
PDF
Hibernate I
PPTX
Entity Framework v1 and v2
PPT
Jdbc Dao it-slideshares.blogspot.com
PPTX
ch02models.pptx
PPTX
ch02models.pptx
PPT
Introduction to odbms
PPT
Utilized Code Gen To Save Our Efforts In Sap Integration
PDF
Free Hibernate Tutorial | VirtualNuggets
PPTX
Domain oriented development
DOCX
Dbms important questions and answers
PPTX
DIGITAL CONTENT for the help of students.pptx
fdocuments.in_introduction-to-ibatis.ppt
Building nTier Applications with Entity Framework Services (Part 1)
Object relationship mapping and hibernate
Building nTier Applications with Entity Framework Services (Part 1)
Object relational database management system
Dev381.Pp
PATTERNS07 - Data Representation in C#
Entity Framework Interview Questions PDF By ScholarHat
Hibernate I
Entity Framework v1 and v2
Jdbc Dao it-slideshares.blogspot.com
ch02models.pptx
ch02models.pptx
Introduction to odbms
Utilized Code Gen To Save Our Efforts In Sap Integration
Free Hibernate Tutorial | VirtualNuggets
Domain oriented development
Dbms important questions and answers
DIGITAL CONTENT for the help of students.pptx
Ad

More from Ranjan Kumar (20)

PPT
Introduction to java ee
PPT
Fantastic life views ons
PPS
Lessons on Life
PPS
Story does not End here
PPS
Whata Split Second Looks Like
PPS
Friendship so Sweet
PPS
Dedicate Time
PPS
Paradise on Earth
PPS
Bolivian Highway
PPS
Chinese Proverb
PPS
Warren Buffet
PPS
Dear Son Dear Daughter
PPS
Jara Sochiye
PPS
Blue Beauty
PPS
Alaska Railway Routes
PPS
Poison that Kills the Dreams
PPS
Horrible Jobs
PPS
Best Aviation Photography
PPSX
Role of Attitude
PPS
45 Lesons in Life
Introduction to java ee
Fantastic life views ons
Lessons on Life
Story does not End here
Whata Split Second Looks Like
Friendship so Sweet
Dedicate Time
Paradise on Earth
Bolivian Highway
Chinese Proverb
Warren Buffet
Dear Son Dear Daughter
Jara Sochiye
Blue Beauty
Alaska Railway Routes
Poison that Kills the Dreams
Horrible Jobs
Best Aviation Photography
Role of Attitude
45 Lesons in Life

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Modernizing your data center with Dell and AMD
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
Chapter 3 Spatial Domain Image Processing.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
NewMind AI Monthly Chronicles - July 2025
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
Modernizing your data center with Dell and AMD
Per capita expenditure prediction using model stacking based on satellite ima...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.

01 Persistence And Orm

  • 1. Object/Relational Persistence and ORM Persistent data in modern object-oriented applications & efficient data management
  • 2. What is persistence? “Persistence” where data (state) outlives the process that created it In object-oriented applications: objects in a system are by default transient some of the objects in the graph are made persistent this subgraph of persistent instances can later be re-created in the same process or another process We need a data model and data store for persistent data... The Relational Data Model and SQL-based Database Management Systems.
  • 3. What is persistence? PERSISTENCE APPROACHES 1. A well-know and widely used DAO design pattern to wide complex JDBC code and no portable SQL from business logic. ORM also uses DAO pattern 2. IBatis is a best persistence layer for legacy systems, Batch processing system and Report generation. JDBC, Spring-JDBC and IBatis(SQL Maps)
  • 4. What is persistence? PERSISTENCE APPROACHES 3. EJB entity beans (CMP) Not fully object-oriented Doesn’t support polymorphic associations and queries Not portable across application servers Not fertilizable. Need to use DTO(Data transfer object or Value objects), and additional load Persistence runs only with EJB container Problems for Test Cases) In EJB3 no CMP, uses ORM for persistence Hibernate is good for JBOSS and major app. Servers Top Link is good for Oracle app. server
  • 5. What is persistence? PERSISTENCE APPROACHES 4. Object-oriented database system (ODBMS) JDO (Java Data Objects) are popular. But, object-databases are not widely adopted. 5. XML Parses Advantages over Serialization but it is just another text file format.
  • 6. What is persistence? PERSISTENCE APPROACHE 6. THE BEST FOR THE MOST PROBLEMS ORM (Object-Relation Model) An API for performing basic CRUD operations on objects of persistent classes A language or API for specifying queries that refer to classes and properties of classes A facility for specifying mapping metadata A technique for the ORM implementation to interact with transactional objects to perform dirty checking, lazy association fetching and other optimization functions.
  • 7. What is persistence? PERSISTENCE APPROACHE 6. THE BEST FOR THE MOST PROBLEMS ORM (Object-Relation Model) ORM advantages: Productivity - no tedious SQL code Maintainability - Fewer lines of code makes the system more understandable Performance Vendor Independence
  • 8. The persistence layer How does the Java application interact with the SQL database? Application layers group related code by concern prevent leakage of concerns have clear interfaces to avoid inter-layer dependencies A typical Layered Architecture and its interface dependencies: Persistence Layer Business Layer Presentation Layer Utility and Helper Classes
  • 9. Tabular Data vs. Domain Model Some applications work exclusively with a tabular representation of persistent data: application works with tabular result sets in all layers straightforward with JDBC and SQL even better with detached RowSet (JSR 114) hard to code re-usable business logic Other applications have an object-oriented domain model : significant business logic, benefits from object-oriented techniques such as inheritance, polymorphism, and patterns greater degree of abstraction of the underlying relational data SQL / JDBC no longer so appropriate certain operations (batch processing, reporting) are more tabular in nature Some applications benefit from both , in different places.
  • 10. Domain Model and the paradigm mismatch Classes implement the business entities of our domain model attributes of the entity are properties of our Java class associations between entities are also implemented with properties Let’s see if there is a problem mapping this to tables and columns... 1 1..* User userName: String address: String billingDetails: Set BillingDetails accountNumber: String accountName: String accountType: String user: User
  • 11. Creating tables for the Domain Model SQL schema design for trivial cases is ... trivial: We’ll see the 5 problems of the O/R paradigm mismatch appear as we gradually make our model more complex… create table USER ( USER_NAME varchar not null primary key, ADDRESS varchar not null) create table BILLING_DETAILS ( ACCOUNT_NUMBER varchar not null primary key, ACCOUNT_NAME varchar not null, ACCOUNT_TYPE varchar not null, USER_NAME varchar foreign key references USER)
  • 12. The problem of granularity should we create a new ADDRESS table? should we create a new SQL data type and change the column? user-defined data types (UDT) are not portable and the standard is weak We usually add new columns to USER with built-in SQL data types: create table USER ( USER_NAME varchar not null primary key, ADDRESS_STREET varchar not null, ADDRESS_CITY varchar not null, ADDRESS_ZIPCODE varchar not null ) User userName: String billingDetails: Set Address street: String city: String zipcode: String
  • 13. The problem of subtypes We create subclasses of BillingDetails : and use polymorphism in Java to implement our billing strategy. How do we represent subtypes in our relational model? 1 1..* User BillingDetails CreditCard Cheque
  • 14. The problem of identity In Java, we have two notions of "sameness" object identity is the memory location of an object, a==b object equality (what is this really?), a.equals(b) In SQL databases, we identify a particular row using the primary key and the table name. Often, this is a (meaningless) surrogate key ! What is the relationship between the three different types of identity in our domain model? Is the surrogate key propagated into the domain model?
  • 15. The problem of associations Object-oriented languages represent entity relationships as object references (pointers) and collections of object references Relational databases represent entity relationships as copies of primary key values referential integrity ensured by foreign key constraints The mismatch: object references are directional, there is no such concept in the relational model many-to-many associations require a link table in relational databases
  • 16. The problem of object graph navigation In Java, we "walk" the object graph by following references: david.getBillingDetails().getAccountName() In SQL, we join tables to get the required data: select * from USER u left outer join BILLING_DETAILS bd on bd.USER_ID = u.USER_ID where u.USERNAME = “david" Avoid the n+1 selects problem by minimizing the SQL required for graph walking!
  • 17. The cost of the mismatch There problems can, at least theoretically, be solved using handwritten SQL/JDBC by writing a lot of tedious code (maybe 30% of your codebase) The “mismatch problem” is real better UDT support in SQL will not solve all the issues not all applications are suitable for table-oriented approaches Is the solution design patterns (DAO) or programming models (EJB entity beans)? "How should we implement the persistence layer in our application?"
  • 18. Object/Relational Mapping Object / Relational Mapping (ORM) solve the mismatch problem in middleware an ORM solution transforms data from the object-oriented representation to the relational representation metadata governs this transformation Elements of an ORM implementation a programming model for the domain objects an API for performing CRUD operations a query language or other query facility a metadata facility