Title: Java Data Objects Write Once Persist Everywhere
1Java Data ObjectsWrite Once - Persist
Everywhere!
- Robin M. Roos
- Principal Consultant
- Ogilvie Partners
- www.OgilviePartners.com
2Introduction
- Robin Roos (Robin_at_OgilviePartners.com)
- Using Java since 1997
- Sun certified
- Programmer, Developer, Architect, WCD
- Java instructor architecture consultant
- Member of JDO Expert Group (JSR12)
- Training course JDO Book
3Background
- JDO Specification...
- Transparent Persistence for Java Objects
- Café June 1999
- Participant Review May 2000
- Public Review July 2000
- 1st Proposed Final Draft May 2001
- Standardized March 2002
4Acknowledgements
- Java
- is a Trade Mark of Sun Microsystems
- JDOQL
- The Query examples are taken from the JDO
Specification - JDO
- would not have happened without the efforts of
the JDO Expert Group
5Agenda
- Part 1 JDO - Write Once, Persist Anywhere
- 1. Overview
- 2. JDO Architecture
- 3. JDO Instance Lifecycle
- 4. Persistent Object Model
- 5. Primary Interfaces and Classes
- 6. Queries with JDOQL
6WARNING
- This presentation contains Java and XML
7Persistence Overview
- Object-Relational Mapping
- Most Java application objects need to be
persisted - Object/Relational impedance mismatch
- Until now, JDBC programming used to map between
rows/columns and object properties - JDBC must be crafted by an SQL expert
- Resulting objects restricted to Relational
persistence
8UML for JDBC Example
9JDBC Pros and Cons
- Advantages
- Application works with any JDBC-compliant
database - Can adapt to existing Customer table structure
- Disadvantages
- Only JDBC-compliant (Relational) databases
- Must write persistence infrastructure taking
resource away from application logic. - Need SQL expertise
- SQL not always portable
10Java Data Objects
- Specification for Transparent Persistence
- Abstract API for persistence services
- Influenced by ODMG and Forté for Java
- Supported by many vendors
- Program to the standard API
- Select a compliant implementation based on
quality of service
11Elementary JDO (1/4)
- Some domain classes capable of persistence
- Write as ordinary classes and compile them.
- Describe their persistence needs in XML
- Enhance by hand or using the enhancer tool
- Provided by your JDO vendor
- Might generates DDL file for relational tables
- Create the tables using the DDL file as required
12Elementary JDO (2/4)
- Some applications read/write Persistent objects
- Lookup or otherwise obtain a factory
- Use PersistenceManagerFactory instance to obtain
a PersistenceManager instance - Invoke methods on the PersistenceManager
- makePersistent() to store a new object
- getExtent() to read lists of objects
13Elementary JDO (3/4)
- Developer does not
- use SQL
- know or worry about table structure
- map objects/attributes to tables/columns
- write infrastructure code
14Elementary JDO (4/4)
- Application is independent of
- JDO Vendor
- OpenFusion JDO from Prism Technologies today
- Xxx JDO from Xxx Corporation tomorrow
- Storage technology
- RDBMS today, ODBMS tomorrow, low-footprint
storage engine next week.
15UML for JDO Example
16Persistence Descriptor (XML)
17Understanding the JDO Example
- Bootstrap the JDO Implementation
- Get a PersistenceManagerFactory
- Get a PersistenceManager
- Create and Persist a new Customer
- Read and display all persistent Customers
- Close Resources
18jdo.properties
- jdo.properties is not part of the JDO standard
- it is used by the helper class JDOBootstrap
- javax.jdo.PersistenceManagerFactoryClasscom.prism
t.j2ee.jdo.PersistenceManagerFactoryImpl - javax.jdo.option.ConnectionUserNamesa
- javax.jdo.option.ConnectionPassword
- javax.jdo.option.ConnectionURLjdbchsqldbhsql//
localhost - javax.jdo.option.ConnectionDriverNameorg.hsqldb.j
dbcDriver
19Bootstrap JDO Implementation
- JDOBootstrap bootstrap new JDOBootstrap()
- PersistenceManagerFactory pmf
- pmf bootstrap.getPersistenceManagerFactory()
- pm pmf.getPersistenceManager()
20Create a new Customer
- Customer c new Customer()
- c.setCustomerNumber(args0)
- c.setName(args1)
- c.setAddress(args2)
- try
- c.setCreditLimit(Double.parseDouble(args3))
-
- catch (NumberFormatException nfe)
- c.setCreditLimit(0)
-
-
-
21Persist the new Customer
- Transaction t pm.currentTransaction()
- t.begin()
- pm.makePersistent(c)
- t.commit()
22Read and display all Customers
- t.begin()
- Extent ext pm.getExtent(Customer.class, true)
- Iterator i ext.iterator()
- System.out.println(Listing all Customers)
- while (i.hasNext())
- System.out.println(i.next())
-
- System.out.println(Done.)
-
23Close Resources
- // close the iterator
- ext.close(i)
- // commit transaction
- t.commit()
- // close the PersistenceManager
- pm.close()
24JDO Pros and Cons
- Advantages
- Application works with any compliant JDO
implementation - RDBMS, OODBMS or lightweight alternative
- Dont write persistence infrastructure just
application logic - No SQL dependency
- Disadvantages
- Mapping to existing relational schema is
described in a vendor-specific manner.
25JDO Instance
- Java language instance of a class which
- usually part of business domain object model
- implements application functionality
- can represent data from a persistent store
- Implement the PersistenceCapable interface
- explicitly via class developer
- implicitly via class Enhancer
26JDO Implementation
- Set of classes implementing JDO Interfaces
- implementations are expected to be appropriate
for one particular storage medium - a generic JDBC-compliant RDBMS, or a particular
RDBMS (optimised at expense of SQL portability) - a particular ODBMS
- a lightweight medium such as binary files
- Applications using JDO portable across different
JDO Vendors implementations
27Enhancer
- Byte-Code Enhancer
- Modifies .class files to facilitate field access
by JDO infrastructure - methods/attributes do not have to be public
- Result implements PersistenceCapable and provides
concrete method implementations - Full binary compatibility
28Transparent Persistence (1/2)
29Transparent Persistence (2/2)
- Persistence by Reachability
- very rarely need to use makePersistent()
- Automatic change tracking
- Automatic synchronization of dirty Instances on
commit - no need to save() changes to persistent
instances
30JDO Environments
- JDO usage is envisaged in 2 environments
- Non-managed Environment
- usage independent of J2EE Application Servers
- all the benefits of transparent persistence
- Managed Environment
- Entity, Session, MessageDriven Beans,
Servlets/JSP - Additional transparency for components use of
infrastructure (transactions, security,
connections)
31Transactional/Persistent
- JDO Instances may be
- Transient or Persistent
- Transactional or Nontransactional
32JDO Identity Mechanisms (1/2)
- Application (Primary Key) Identity
- values in the instance determine its identity
- Data Store Identity
- independent of any instance values
- Non-durable JDO Identity
- uniqueness is guaranteed in the JVM but is not
supported in the data store.
33Specifying JDO Identity
34Specifying Application Identity
- identity-typeapplication
- objectid-classop.PersonKey
-
-
35Inheritance
nameProduct persistence-capable-superclass
AbstractItem /
36Collection Example
- transient Collection orders
- orders new HashSet()
-
-
- nameorders
- persistence-modifier persistent
-
-
37Required Lifecycle States
- Transient
- Persistent-New
- Persistent-New-Deleted
- Hollow
- Persistent-Clean
- Persistent-Dirty
- Persistent-Deleted
38Required State Transitions
39JDO Exception Hierarchy
40Primary Interfaces
- JDOHelper
- PersistenceManagerFactory
- PersistenceManager
- Extent
- Transaction
- Query
- (PersistenceCapable)
41Getting to the First Object
- Application may access a closure of instances
navigated to from one instance - 3 ways to get the first instance
- PersistenceManager.getObjectById()
- must be able to construct the Object ID
- PersistenceManager.getExtent()
- must navigate extent of all persistent instances
- JDOQL
42JDO Query Language
- JDO removes SQL-dependencies for manipulating
persistent data - JDOQL is a data store-independent Java-like query
language - Java developers no longer require SQL for queries
either - Efficient architecture (no instantiation)
- Future migration to ODBMS is seamless
43Query Structure
- Candidate class
- Candidate collection or extent
- Java-like Syntax for
- Variable declarations
- Parameter declarations
- Filter expression
- Import declarations
- Ordering declarations
44Supported Operators
45Query Execution
- Parameters passed to execute methods are for this
execution only - Specify parameters in sequence of declaration
- execute()
- execute(Object p1)
- execute(Object p1, Object p2)
- execute(Object p1, Object p2, Object p3)
- executeWithArray(Object a)
- executeWithMap(Map m)
46Query Examples
- 5 examples follow using the classes described
below
47Query Example 1
- Basic query
- Select all Order instances where the orderValue
is greater than the constant 250.
48Query Example 1 - Code
- Extent extOrder pm.getExtent(Order.class,
true) - String filter orderValue 250
- Query q pm.newQuery(extOrder, filter)
- Collection results (Collection) q.execute()
49Query Example 2
- Basic query with ordering
- This query selects all Order instances where the
orderValue is greater than the constant 250, and
returns a Collection ordered according to
orderDate descending
50Query Example 2 - Code
- Extent extOrder pm.getExtent(Order.class,
true) - String filter orderValue 250
- Query q pm.newQuery(extEmployee, filter)
- q.setOrdering(orderDate descending)
- Collection results (Collection) q.execute()
51Query Example 3
- Parameter passing
- This query selects all Order Instances where the
orderValue is greater than the value passed as a
parameter.
52Query Example 3 - Code
- Extent extOrder pm.getExtent(Order.class,
true) - String filter orderValue val
- Query q pm.newQuery(extOrder, filter)
- q.setOrdering(orderDate descending)
- q.declareParameters(Double val)
- Collection results
- results (Collection) q.execute(new Double(250))
53Query Example 4
- Navigation through single-valued field
- This query selects all Order instances from the
candidate collection where the value of the
address field in the corresponding Customer
instance equals the value passed in as a
parameter.
54Query Example 4 - Code
- Extent extOrder pm.getExtent(Order.class,
true) - String filter customer.address addr
- Query q pm.newQuery(extOrder, filter)
- q.declareParameters(String addr)
- Collection emps (Collection) q.execute(London)
55Query Example 5
- Navigation through multi-valued field
- This query selects all Customer instances where
the orders collection contains at least one Order
instance having an orderValue greater than the
value passed in as a parameter.
56Query Example 5 - Code
- String filter, vars, param
- Class custCls Customer.class
- Extent extCustomer pm.getExtent(custCls, true)
- vars Order o
- filter orders.contains(o) o.orderValue
val - param Float val
- Query q pm.newQuery(extCustomer, filter)
- q.declareParameters(param)
- q.declareVariables(vars)
- Double searchVal new Double(250)
- Collection result (Collection)
q.execute(searchVal)
57But Can I Use JDO Today
?
58Commercial Implementations
- enJin by Versant Software
- FastObjects by Poet Software
- FrontierSuite by ObjectFrontier
- IntelliBO by Signsoft GmbH
- JDO Genie by Hemisphere Technologies
- JRelay by Object Industries GmbH
- Kodo JDO by Solarmetric
- LiDO by LIBeLIS
- OpenFusion JDO by Prism Technologies
- Orient by Orient Technologies
59Managing Complexity
Just one of many diagrams that comprise a
non-trivial object model, designed in Together
6.0 and persisted with JDO as part of a recent
Ogilvie Partners client engagement
60Conclusion
- JDO
- provides significant development time savings
- is easier than JDBC and can be faster!
- prepared batched statements
- is more reliable than JDBC
- particularly for complex domain object models
- integrates fully with all J2EE components
- Vendors will still let you use SQL if you prefer!
61Ogilvie Partners JDO Training
- 2-day labs-based vendor-independent
- Public Training
- UK
- Milton Keynes
- Edinburgh
- USA
- New York
- Washington
- Others
- Dedicated Training
- Available world-wide
62Ogilvie Partners Consultancy
- Consultancy
- JDO applicability to your projects
- JDO Implementation selection
- Architecture design
- Domain object modelling
- Mentoring
- Meeting JDO-based learning objectives whilst
working with developers on their project work
63Ogilvie Partners The JDO Book
- Java Data Objects
- Written by Robin Roos
- Published by Addison-Wesley Publishers Ltd
- ISBN 013066839, Available from August 2002
- Foreword by Craig Russell, JDO Spec Lead
- Pre-order at Amazon.co.uk now
- Complete coverage of JDO, many examples
- including J2EE and JDOQL
- prerequisite knowledge of the Java language
64(No Transcript)
65Online JDO-Related Forums
- JavaDataObjects at Yahoo!Groups
- www.groups.yahoo.com/group/JavaDataObjects
- JDOcentral
- www.JDOcentral.com
- Lots more information on our website
- www.OgilviePartners.com
- Contact me
- Robin_at_OgilviePartners.com
66www.OgilviePartners.com
67(No Transcript)
68Rewriting the J2EE Blueprints
- Integrating JDO with J2EE Architectures and
Components
69Client-Server Architecture
70RMI Server Architecture
Before JDO Today
71J2EE Blueprint - Original
72J2EE Blueprint - Session Façade
73J2EE Blueprint - Real World
74JDO in the J2EE Architecture
75Reassuring Entity Bean Lovers
JDO as delegate for BMP
76(No Transcript)
77(No Transcript)