Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 1
Mapping Java Objects to XML and Relational DatabasesO-R and O-X Mapping
Donald SmithTechnology Director, Oracle Corporationblog: jroller.com/page/dsmith
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 2
Speaker’s Qualifications
Decade of experience in OO PersistencePresented at Java One, Oracle World, OOPSLA, JAOO, Sun Tech Days, TheServerSide Symposium, etc.Author of numerous articles on persistence challenges
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 3
About the Audience…
Who considers themselves first and foremost to be a DBA or “Database expert”?Who considers themselves first and foremost to be a Java and/or Web Services developer?Who considers themselves first and foremost to be an Architect?Who considers themselves first and foremost to be a manager, and will you admit it?
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 4
Goal
Discuss and demonstrate the challenges of mapping Java Objects to Relational Databases and XML documents
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 5
AgendaQuick Review of Java Mapping
O-R and O-X OverviewTerminology
O-R MappingDirect, 1-1, Aggregate, 1-M, M-M, Inheritance
O-X MappingDirect, Composite, Transformation, Inheritance
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 6
AgendaQuick Review of Java Mapping
O-R and O-X OverviewTerminology
O-R MappingO-X Mapping
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 7
Impedance Mismatch
Difference in relational, XML and object technology known as “impedance mismatch”Challenging problem
Requires relational, XML and object expertise
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 8
O-R Impedance MismatchFactor J2EE Relational DatabaseLogical Data Format
Objects, methods, inheritance
Tables, SQL, stored procedures
Scale Hundreds of megs Gigabytes, terabytes
Relationship Memory references Foreign keysUniqueness Internal object identity Primary keysKey Skills Java development,
object modelingSQL, Stored Procedures, data management, Data modeling
Tools IDE, Source code management, Object Modeler
Schema designer, query manager, performance profilers, database config
Corporate Org. Structure
“Newer technology”often with weak organizational ties to database mgmt
Often mature infrastructure with significant legacy considerations
Tech
nica
lB
usin
ess
Pol
itica
l
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 9
O-X Impedance MismatchFactor J2EE XMLLogical Data Format
Objects, methods, inheritance
XSD, XPATH
Scale Hundreds of megs Depends
Relationship Memory references Generally use AggregationUniqueness Internal object identity Unique IdentifierKey Skills Java development,
object modelingXML, XSD, XPATH, XQUERY
Tools IDE, Source code management, Object Modeler
XML Design Tools and Viewers
Corporate Org. Structure
Often integrating or using legacy application code
High expectations of data portability
Tech
nica
lB
usin
ess
Pol
itica
l
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 10
AgendaQuick Review of Java Mapping
O-R and O-X OverviewTerminology
O-R MappingO-X Mapping
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 11
JDBC
Java standard for accessing data sourcesSimply – data source connectionOK with:
“Window on data” applicationsBusiness logic entrenched on databaseJava nothing more than GUI tool
SQLrows
JDBC
SQLrows
Java & Java & Web Web
ServicesServices
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 12
Persistence LayerAbstract persistence details from application layerRequires Object to Data source mapping…
SQLrows
Objects
Persistence Layer
Objectsobject-level
querying and creationresults are objects
results arereturned as
raw data
API uses SQLor databasespecific calls
object creation and updates through object-level API
Java & Java & Web Web
ServicesServices
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 13
O-R Mapping Tools
O-R Mapping Tools help you manage the mapping’s meta data
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 14
AgendaQuick Review of Java Mapping
O-R and O-X OverviewTerminology
O-R MappingO-X Mapping
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 15
3 Levels of XML Representation
XML ParserO-X Data Converter
XMLDocument
UnmanagedObject
O-X Persistence Manager
XDB EIS
JDBC J2C
DOM ManagedObject/EJB
File
WebService BPM JMS RDB
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 16
XML ParserJAXP – Java API for XML Parsing
DOMSAX
Very low levelAkin to straight using straight JDBC for database interactionsUseful for simple and raw GUI based apps where a business model is overkill
XML Parser
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 17
O-X Data ConverterConverts XML data to Java objects and vice-versaAccessed by applications through Marshal/Unmarshal interfaceUsually for non-transactional data sourcesJAXB implementations
O-X Data Converter
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 18
O-X Data Converter Limitations
Beware of your JAXB implementation…Most generate Java classes from XML Schema –static, inflexibleMost offer no control over the mappingsCan’t use your own Java classesApplication code is tightly coupled to a specific XSD
Usually no GUI tools to do mappingsConversion only, no run-time manager available for transactional data sourcesHomogeneous data support
Specific interfaces and generation for XML
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 19
O-X Persistence ManagerFlexible mapping, developers control how objects are mapped to XML – “meet in the middle”
Can use developer-defined Java classesIndependence between object model and XML schemaBusiness logic can be safely added into Java modelClasses can be mapped to multiple schemas – vice versaCan be JAXB compliant
O-X Persistence Manager
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 20
O-X Persistence Manager
Support complex XML mappingsPositional, path informationExamples coming…
May provide visual mapping interfaceMay support other data sources – relational and EIS
O-X Persistence Manager
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 21
O-X Persistence ManagerPersistence manager functionality may be required for transactional XML data sources such as EIS systems, XML databases.Provides additional capabilities on top of data conversion such as:
CachingQueryingTransactionsConcurrency
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 22
O-X Mapping
Map Object Model to XSDEither code gen XSD from Object Model, vice versa, or “meet in the middle”
Java IDEJava source
MappingTool
XSD
MappingXML
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 23
O-X Mapping Tools
O-X Mapping Tools help you manage the mapping’s meta data
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 24
O-R versus O-XO-X
Much more aggregation (aka “denormalized”)Relationships represented in structure of documentRelated data usually contained in single “bite”• “Less Chatty”
O-RRelationships represented by data in table (FK)Frequent round-trippingData storage and practices more standardized
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 25
AgendaQuick Review of Java Mapping
O-R and O-X OverviewTerminology
O-R MappingO-X Mapping
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 26
Mapping TerminologyDirect Mappings
Field in Object model maps to field in datasource• May require conversion, aggregation or serialization
Relationship MappingsThought of always as uni-directional• “Bi-directional” relationships are two distinct
mappings
Source is the originator of the relationshipTarget is referenced object or set of objects from the source
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 27
Mapping Terminologypublic class Source {private String directAttribute;private Target target;private Collection targetCollection;
}
Source
String:directAttribute
Targettarget
targetCollection
*
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 28
AgendaQuick Review of Java MappingO-R Mapping
Direct, 1-1, Aggregate, 1-M, M-M, Inheritance
O-X Mapping
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 29
Simple Direct Mapping
Customer
id: intname: StringcreditRating: int
CUSTID NAME C_RATING
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 30
Direct Conversion Mappings
Customer
id: intname: Stringgender: String
CUST
ID NAME GENDERFFemaleMMaleRO
Employeeid: intname: StringworkTerm: Date[]
EMPLOYEE
ID NAME START END
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 31
1-1 Object ModelCustomer
id: intname: StringcreditRating: int
Address
id: intcity: Stringzip: String
1:1 Relationship
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 32
Typical 1-1 Relationship
CUST
ID NAME A_IDC_RATINGADDR
ID CITY ZIP
CUSTID NAME A_ID
ADDRID CITY ZIP
7 Don 3 3 Ottawa K2J4X717 Toronto K1P2T213 Trisha 17
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 33
1-1 FK in Target Table
CUST
ID NAME C_RATE C_ID
ADDR
ID CITY ZIP
CUSTID NAME C_ID
ADDRID CITY ZIP
7 Don 73 Ottawa K2J4X717 Toronto K1P2T213 Trisha 13
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 34
Association TableCUST
ID NAME C_RATING
ADDR
ID CITY ZIP
A_ID
CUST_ADDR
C_ID
CUSTID NAME
C_ID A_ID
ADDRID CITY ZIP
7 Don7 3
3 Ottawa K2J4X717 Toronto K1P2T213 Trisha
13 17
CUST_ADDR
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 35
Aggregation
CUST
ID NAME CITY ZIPC_RATING
CUSTID NAME CITY ZIP7 Don Ottawa K2J4X7
Toronto K1P2T213 Trisha
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 36
Multiple Table Mappings
CUST
ID NAME A_ID
ADDR
ID CITY ZIP
CUST_CREDIT
ID C_RATING
CUST
ID NAME
ADDR
ID CITY ZIP
CUST_CREDIT
ID C_RATING A_ID
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 37
Multiple Table Mappings
CUST
ID NAME
CUST_CREDIT
ID C_RATING
ADDR
ID CITY ZIP C_ID
CUSTID NAME
CUST_CREDITID C_RATING
ADDRID CITY ZIP C_ID
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 38
Multiple Table Mappings
CUST
ID NAME
ADDR
ID CITY ZIP
CUST_CREDIT
ID C_RATING A_IDCC_ID
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 39
1-M Object ModelCustomer
id: intname: StringcreditRating: int
Phoneid: intString: typeString: areaCodeString: number
1:M Relationship*
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 40
1-M Relationship
CUST
ID NAME C_RATING PHONE_NUMBER
ID TYPE A_CODE PNUMB C_ID
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 41
1-M Relationship Trick
Mapping a 1-1 “back reference” from the target of the 1-M to the sourceAllows persistence layer to more easily handle the foreign key
Customer
id: intname: StringcreditRating: int
Phoneid: intString: typeString: areaCodeString: number
*
1
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 42
1-M Relationship Trap!What if order is important?Specify field to order by
Customer
id: intname: StringcreditRating: int
Phoneid: intString: typeString: areaCodeString: number
*
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 43
1-M Relationship Trap!What if collection indexis the order criteria?
Customer
id: intname: StringcreditRating: int
Phoneid: intString: typeString: areaCodeString: number
*
:Customereename = “Don”
:Collectionee
:Phone1eetype = ‘home’numb = 45367
:PhoneNeetype = ‘cell’numb = 6573
…
1 n
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 44
1-M Relationship Trap!Challenging problem because “state” from Java utility class needs to be persistedBest solution is to find or make a persistent attribute of the target the order by fieldRegardless, database requires SOMETHING to order by
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 45
1-M Relationship Trap!
:Employeeeename = “Don”
:Collectionee
:Phone1eetype = ‘home’numb = 45367
:Phone1eetype = ‘cell’numb = 6573
…
1 n
EMPID NAME
PHONEID TYPE NUMBER
7 Don
3 home 4536717 cell 6573
ORDER13
15 work 99882 2
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 46
M-M Object ModelEmployee
id: intname: String
Project
id: intname: Stringbudget: double
M:M Relationship *
*
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 47
M-M RelationshipEMP
ID NAME
PROJECT
ID NAME BUDGET
P_ID
EMP_PROJ
E_ID
EMPID NAME
E_ID P_ID
PROJECTID NAME BUDGET
7 Don7 3
3 Tiger 5,000,00017 Dragon 1,000,00013 Mike
7 17
EMP_PROJ
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 48
M-M Relationship Trap!
EMP
ID NAME
PROJECT
ID NAME BUDGET
P_ID
EMP_PROJ
E_ID ROLE
Common challenge – RDBs often intertwine data and relationships. Where would this go in object model?
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 49
M-M Relationship Trap!In relational world, putting “data” on a relationship is not uncommonNot possible in JavaMust store data in an object – requires some re-thinking of Object model
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 50
M-M Trap SolutionEmployee
id: intname: String
Project
id: intname: Stringbudget: double
*
*
Responsibility
roleName: Stringemp: Employeeproj: Project
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 51
Direct Collection Mapping
:Employeeeename = “Don”tasks = {“schmooze”, “gossip”, “coffee”}
EMPID NAME
EMP_TASKID TASK
7 Don
7 schmooze
7 gossip
7 coffee
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 52
Direct Collection IssuesInefficient for large direct collections
Changes require lots of “change detection” analysis, or massive purge and insert on database
Only useful for PRIMITIVES, more than 1 piece of date requires 1-M mappingConsider Maps of Primitives…
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 53
Direct Map Mapping
:Employeeeename = “Don”contact = {“email” -> “[email protected]”, “home” -> “555-1212”,“cell” -> “555-2323”}
EMPID NAME
CONTACTID KEY
7 Don
7 email
7 home
7 cell
VALUE
555-1212
555-2323
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 54
InheritanceVery straight forward at first, but can become challenging because of the number of possible solutionsCompounded with relationshipsCan be source of contention with DBA –generally leads to lots of joining on queries!
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 55
“Leaf Table Mapping”
Sub1sub1var: String
Root {a}
id: intvar: String
Sub2 {a}sub2var: int
Sub22sub22var: String
Map only concrete classes
SUB1ID VAR
SUB22
7 FooSUB1VAR
Pish
ID VAR13 Tim
SUB2VAR677
SUB22VARFlim
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 56
“Leaf Table” IssuesHow do you query for the Root class?Data is not normalized
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 57
“All Table Mapping”
Sub1sub1var: String
Root {a}
id: intvar: String
Sub2 {a}sub2var: int
Sub22sub22var: String
Map all classes ROOT
ID VAR
SUB2
7 Foo13 Bar
ID13
SUB2VAR677
SUB22VARFlim
SUB1ID7
SUB1VARPish
ID13
SUB22
Problem! How to tell type?!
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 58
Identifying Type
Sub1sub1var: String
Root {a}
id: intvar: String
Sub2 {a}sub2var: int
Sub22sub22var: String
Map all classes ROOT
ID VAR
SUB2
7 Foo13 Bar
ID13
SUB2VAR677
SUB22VARFlim
SUB1ID7
SUB1VARPish
ID13
SUB22
Need Type Identifier!
TYPES1
S22
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 59
Identifying Type
ROOTID VAR7 Foo
13 Bar
Lots of strategies exist…TYPE
S1S22 ROOT
ID VAR7 Foo13 Bar
TYPEcom.foo.Sub1
com.foo.Sub22
ROOTID VAR7 Foo
13 Bar
if id < 10 type = Sub1
else type = Sub22
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 60
“All Table” IssuesLots of joiningType identification can be inefficient
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 61
“Single Table Mapping”
Sub1sub1var: String
Root {a}
id: intvar: String
Sub2 {a}sub2var: int
Sub22sub22var: String
Map to one table
ROOTID VAR7 Foo
13 Bar
SUB1VARPishnull
SUB2VARnull677
SUB22VARnullFlim
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 62
Identifying Type
Sub1sub1var: String
Root {a}
id: intvar: String
Sub2 {a}sub2var: int
Sub22sub22var: String
ROOTID VAR7 Foo13 Bar
SUB1VARPishnull
SUB2VARnull677
SUB22VARnullFlim
TYPES1
SUB22
if SUB1VAR != null type = Sub1
else type = Sub22
Two options for type detection
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 63
“Single Table” IssuesDBA will freak out – de-normalized databaseType identification can be inefficient
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 64
“Combination Table Mapping”
Sub1sub1var: String
Root {a}
id: intvar: String
Sub2 {a}sub2var: int
Sub22sub22var: String
Map appropriate classes
ROOTID VAR7 Foo13 Bar
SUB2VAR677
SUB22VARFlim
SUB1ID7
SUB1VARPish
ID13
SUB22
TYPES1
S22
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 65
Which Strategy to Use?Leaf Table
When never querying at abstract class levelAll Table
When not doing much querying• Too many joins
Single TableWhen one subclass is much more prevalent, and your DBA doesn’t care about normalization
CombinationUse to optimize above situations
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 66
Wouldn’t It Be Cool?
Sub1sub1var: String
Root {a}
id: intvar: String
Sub2 {a}sub2var: int
Sub22sub22var: String
Map to view for reads…ROOT (View)ID VAR7 Foo13 Bar
SUB1VARPishnull
SUB2VARnull677
SUB22VARnullFlim
ROOTID VAR SUB27 Foo13 Bar ID
13SUB2VAR
677
SUB22VARFlim
SUB1ID7
SUB1VARPish ID
13
SUB22
Map to tables for writes…
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 67
AgendaQuick Review of Java MappingO-R MappingO-X Mapping
Direct, Composite, Transformation, Inheritance
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 68
Example Object ModelAddress
addressee: Stringcity: Stringstreet1: Stringstreet2: Stringstate: Stringcountry: StringzipCode: String
LineItemlineNumber: longitemName: Stringquantity: longitemPrice: BigDecimal
Order
id: longorderedBy: String address
1
* lineItems
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 69
Direct Mapping
<ORDER ORDER_ID="1234"><ORDERED_BY>Jane Doe</ORDERED_BY><ADDRESS>
...</ADDRESS><LINES>
...</LINES><LINES>
...</LINES>
</ORDER>
: Order
id = 1234orderedBy = “Jane Doe”
: Address
: LineItem : LineItem
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 70
Composite Object Mapping
<ORDER ORDER_ID="1234"><ORDERED_BY>Jane Doe</ORDERED_BY><ADDRESS>
...</ADDRESS><LINES>
...</LINES><LINES>
...</LINES>
</ORDER>
: Order
id = 1234orderedBy = “Jane Doe”
: Address
: LineItem : LineItem
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 71
Composite Collection Mapping
<ORDER ORDER_ID="1234"><ORDERED_BY>Jane Doe</ORDERED_BY><ADDRESS>
...</ADDRESS><LINES>
...</LINES><LINES>
...</LINES>
</ORDER>
: Order
id = 1234orderedBy = “Jane Doe”
: Address
: LineItem : LineItem
Possible <lineitems> tag here
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 72
Positional InformationGood O-X Support
JAXB/Class Generation Today…
<ADDRESS><ADDRESSEE>Jane Doe</ADDRESSEE><STREET>123 Some St.</STREET><STREET>Apt. #123</STREET>
…</ADDRESS>
: Address
addressee = “Jane Doe”street = “123 Some St.”apartment = “Apt. #123”…
<ADDRESS><ADDRESSEE>Jane Doe</ADDRESSEE><STREET>123 Some St.</STREET><STREET>Apt. #123</STREET>
…</ADDRESS>
: Address
addressee = “Jane Doe”street = {“123 Some St.”,
“Apt. #123”}…
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 73
Path InformationGood O-X Mapping: LineItem
lineNumber = 1itemName = “Pens”itemPrice = 2.50quantity = 50
<LINES LINE_NO="1"><ITEM>
<NAME>Pens</NAME><PRICE>2.50</PRICE>
</ITEM><QUANTITY>50</QUANTITY>
</LINES>
<LINES LINE_NO="1"><ITEM>
<NAME>Pens</NAME><PRICE>2.50</PRICE>
</ITEM><QUANTITY>50</QUANTITY>
</LINES>
: Lines
lineNumber = 1quantity = 50
: Item
name = “Pens”price = 2.50
JAXB/Class Generation Today…
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 74
Direct Collection Mapping
<EMPLOYEE><TASK>this</TASK><TASK>that</TASK><TASK>other thing</TASK>
</EMPLOYEE>
: Employee
tasks = {“this”, “that”, “other thing”}
• Compare with Positional Mapping
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 75
Transformation Mapping
<EMPLOYEE><START-TIME>9:00:00</START-TIME><END-TIME>17:00:00</END-TIME>
</EMPLOYEE>
: EmployeenormalHours
= {9am,5pm}
: FieldTransformer
: FieldTransformer
Marshal (Write)
: AttributeTransformer
<EMPLOYEE><START-TIME>9:00:00</START-TIME><END-TIME>17:00:00</END-TIME>
</EMPLOYEE>
: EmployeenormalHours
= {9am,5pm}
Unmarshal (Read)
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 76
Conversion Mapping
<EMPLOYEE><GENDER>F</GENDER>
</EMPLOYEE>
: Employee
gender = “Female” “Female” to “F”“Male” to “M”
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 77
Simple Type Translator
<element name=“AREA-CODE” type=“anySimpleType”/><element name=“NUMBER” type=“anySimpleType”/>
PhoneNumberareaCode: Objectnumber: Object
Class Model
<PHONE-NUMBER><AREA-CODE xsi:type=“xsd:int”>613</AREA-CODE><NUMBER xsi:type=“xsd:string”>KL51234</NUMBER>
</PHONE-NUMBER>
: PhoneNumberareaCode = 613number = “KL51234”
Instance Model
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 78
Namespace Support
<abc:EMPLOYEE xmlns:abc=“http://X” xmlns:def=“http://Y”><def:FIRST-NAME>Jane</def:FIRST-NAME><def:LAST-NAME>Doe</def:LAST-NAME>
</abc:EMPLOYEE>
<ns1:EMPLOYEE xmlns:ns1=“http://X” xmlns:ns2=“http://Y”><ns2:FIRST-NAME>Jane</ns2:FIRST-NAME><ns2:LAST-NAME>Doe</ns2:LAST-NAME>
</ns1:EMPLOYEE>
: Employee
firstName = “Jane”lastName = “Doe”
Employee Default Root = abc:EMPLOYEEXPath firstName = def:FIRST-NAMEXPath lastName = def:LAST-NAME
Namespace Resolver:abc = http://Xdef = http://Y
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 79
Inheritance<element name=“ADDRESS” type=“ADDRESS-TYPE”/><complexType name=“ADDRESS-TYPE”>
…..</complexType><complexType name=“CANADIAN-ADDRESS-TYPE>
<complexContent><extension base=“ADDRESS-TYPE”>
…</extension>
</complexContent></complexType>
Address
street: Stringcity: String
CanadianAddress
province: StringpostalCode: String
ClassModel
: CanadianAddress
street = “123 Any St.”city = “Ottawa”province = “ON”postalCode = “A1B 2C3”
<ADDRESS xsi:type=“CANADIAN-ADDRESS-TYPE”><STREET>123 Any St.</STREET><CITY>Ottawa</CITY><PROVINCE>ON</PROVINCE><POSTAL-CODE>A1B 2C3</POSTAL-CODE>
</EMPLOYEE>
InstanceModel
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 80
Conclusions
Much more to O-R and O-X Mapping than first meets the eyeO-R and O-X Share several common challenges, but each have their own unique perspectives
Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, Oracle Corporation
Donald Smith — Mapping Java Objects to XML and Relational Databases: O-R and O-X Mapping Page 81
Recommended Sessions…Donald Smith
Impact of Tiger on Java Persistence
Mike KeithStandards Based Persistence through Web ServicesEvolution of the EJB Entity
Neil GrahamLower-level Data Manipulation with J2SE 5.0
Matthew Wakeling[Advanced Techniques | Performance] in Object Warehousing