Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Introduction to a framework in Java used to persist object into some kind of store, e.g. database.

Published in: Technology
  • Be the first to comment


  1. 1. JDO – Java Data ObjectBjörn Granvik, Jayway
  2. 2. OverviewAn introduction to JDO• Goals, pros, cons• Terms• Enhancement process• Classes & interfaces• Code• A comparison or two
  3. 3. What is JDO?• “Write once, persist everywhere!”• Standard for Java object persistence
  4. 4. JDO Goals• Java-centric view of persistent information• Transparent• Data store independence– Relational, object, hierarchical, file ...• Use in a range of implementations– Embedded, client/server, application server
  5. 5. JDO Terms• JDO instance• JDO identity• Persistence Manager• Persistence Capable• Transient vs. persistent instance• JDO enhancer• (Non)Managed• JDOQL
  6. 6. (Non)managed• Managed mode – application serverServer handles connections, poolingtransactions, security etc• Nonmanaged mode – 1 tier, 2 tier etcYou handle connections and so onCaching included, life cycle and persistencehandled
  7. 7. Architecture OverviewNonmanaged JDO
  8. 8. Class enhancer
  9. 9. A life cycle
  10. 10. What’s in an interface?• PersistenceManagerFactory• PersistenceManager• Transaction• Extent• Query• PersistenceCapable• InstanceCallbacks• JDOHelper• JDO Exception Classes...
  11. 11. PersistenceManagerFactory• To get a persistence manager instance
  12. 12. PersistenceManager• Primary interface for JDO-awareapplication components• Provides:– Cache management– Extent Management– JDO Identity Management– Life cycle management– Query management– Transaction management
  13. 13. Transaction• Data store Transaction Management• Optimistic Transaction Management• In a non-managed environment, a localtransaction is associated with the PM• In a managed environment, a usertransaction or a local transaction isassociated with the PM
  14. 14. Extent• Logical view of all persistent instances of agiven persistence-capable class• Used to specify the candidate objects to aQuery• Later some code...
  15. 15. Query• Goals– Query language neutrality– Optimization to specific query language– Accommodation of multi-tier architectures– Large result set support– Compiled query support• Data store Transaction Management• Optimistic Transaction Management
  16. 16. PersistenceCapable• JDO instances implement thePersistenceCapable interface– Provides the management view of user definedpersistence-capable classes– Persistent state of the object must berepresented entirely by the state of its Javafields prior to being loaded into the executionenvironment– Depends on the persistence manager forservices such as transaction and life cycle
  17. 17. InstanceCallbacks• Persistence capable classes shouldimplement this interface if they wantinstance callbacks– jdoPostLoad()– jdoPreStore()– jdoPreClear()– jdoPreDelete()
  18. 18. JDOHelper• Utility classes for state inspection ofpersistence capable classes
  19. 19. Example: UMLNow some code ...brace yourselves :-)
  20. 20. Example: Fleetpublic class Fleet {private List vehicles = new Vector();public void addVehicle(Vehicle vehicle) {vehicles.add(vehicle);}public Iterator getVehicles() {return vehicles.iterator();}public String toString() {StringBuffer buffer = new StringBuffer("Fleet:n");Iterator iter = getVehicles();while (iter.hasNext()) {buffer.append("t" + + "n");}return buffer.toString();
  21. 21. Example: Vehiclepublic class Vehicle {private int numberOfWheels;public Vehicle(int numberOfWheels) {this.numberOfWheels = numberOfWheels;}public int getNumberOfWheels() {return numberOfWheels;}}
  22. 22. Example: Bicyclepublic class Bicycle extends Vehicle {private String model;public Bicycle(String model) {super(2);this.model = model;}public String toString() {return "Bike: Model " + model;}}
  23. 23. Example: MotorVehiclepublic class MotorVehicle extends Vehicle {private Engine engine;public MotorVehicle(int numberOfWheels, Engine engine) {super(numberOfWheels);this.engine = engine;}public String toString() {return "MotorVehicle With " + getNumberOfWheels()+ " Wheels. " + engine;}}
  24. 24. Example: Enginepublic class Engine {private int numberOfCylinders;public Engine(int numberOfCylinders) {this.numberOfCylinders = numberOfCylinders;}public int getNumberOfCylinders() {return numberOfCylinders;}public String toString() {return numberOfCylinders + " Cylinder Engine.";}}
  25. 25. Example: package.jdo<?xml version="1.0"?><jdo><package name="com.jdodemo"><class name="Engine"/><class name="Vehicle"/><class name="Bicycle"persistence-capable-superclass="Vehicle"/><class name="MotorVehicle"persistence-capable-superclass="Vehicle"/><class name="Fleet"><field name="vehicles"><collection element-type="Vehicle"/></field></class></package></jdo>
  26. 26. Example: Seed, set it up/* */importcom.solarmetric.kodo.impl.jdbc.JDBCPersistenceManagerFactory;public class SeedDatabase {public static void main(String[] args) {Fleet fleet = new Fleet();fleet.addVehicle(new Bicycle("Schwinn"));fleet.addVehicle(new Bicycle("Giant"));fleet.addVehicle(new MotorVehicle(4, new Engine(8)));fleet.addVehicle(new MotorVehicle(2, new Engine(4)));fleet.addVehicle(new MotorVehicle(4, new Engine(4)));...
  27. 27. Example: Seed, wrap it upPersistenceManager pm =new JDBCPersistenceManagerFactory().getPersistenceManager();// begin a transaction...Transaction transaction = pm.currentTransaction();transaction.begin();// persist the;// commit the transaction...transaction.commit();// close the;
  28. 28. Example: List all...public class ListAll {...Extent ext = manager.getExtent( Vehicle.class, true );Query query = manager.newQuery( ext, "" );Collection vehicles = (Collection)query.execute();Iterator iterator = vehicles.iterator();while (iterator.hasNext()) {Vehicle vehicle = (Vehicle);System.out.println("vehicle = " + vehicle);}...
  29. 29. Example: Resultc:> java ListAllvehicle = Bike: Model Schwinnvehicle = Bike: Model Giantvehicle = MotorVehicle With 4 Wheels. 8 Cylinder Engine.vehicle = MotorVehicle With 2 Wheels. 4 Cylinder Engine.vehicle = MotorVehicle With 4 Wheels. 4 Cylinder Engine.= Polymorphism
  30. 30. Example: List four cylinders...public class ListFourCylinderVehicles {public static void main(String[] args) {...Extent ext = manager.getExtent(MotorVehicle.class, true);// only retrieve vehicles with 4 cylinders...Query query = manager.newQuery( ext,"engine.numberOfCylinders == 4");Collection vehicles = (Collection) query.execute();Iterator iterator = vehicles.iterator();while (iterator.hasNext()) {Vehicle vehicle = (Vehicle);System.out.println("vehicle = " + vehicle);}...JDOQL
  31. 31. What is JDOQL?• ”Query language”• Not like SQL – more like Java.• Modeled after Java boolean expressions
  32. 32. Example: Resultc:> java ListFourCylinderVehiclesvehicle = MotorVehicle With 2 Wheels. 4 Cylinder Engine.vehicle = MotorVehicle With 4 Wheels. 4 Cylinder Engine.= No bicycles or 8cylinder wheels
  33. 33. JDO vs. CMPJDO CMPRemote behaviour Managed relations Method security/transaction Encapsulation Inheritence Polymorphic references Field access  Abstract get/setCollection, Set  List, Array, Map OptionalOperating environment 1-tier, 2-tier, app.serverweb- and app.serverMetadata Identify classes Identify classes,defaults on field and fieldsrelationshipts and relationshipsTrans synch callbacks 
  34. 34. JDO vs. CMP cont’dQuery language:JDOQL – modeled after Java boolean expressionsEJBQL – modeled after SQLRequired classes/interfacesJDO Persistent classCMP EJBHome/EJBLocalHomeremote/local interfaceAbstract beans must implement EJBEntityBeanRequired lifecycle methodsJDO no-arg constructorCMP setEntityContext, unsetEntityContext, ejbActivate, ejbPassivate,ejbLoad, ejbStore, ejbRemove=ValueObject
  35. 35. JDO – preferred setup?• JDO for persistence behind EJB sessionbean (façade)• Split application server dependency for:– faster development– faster unit testing– more dynamic architecture (combine 2 tier withapplication server setup)• Separate database dependency for fasterunit testing
  36. 36. JDO Pros• Transparent persistence mechanism• Object oriented = Java friendly• Multi solution architecture friendly• Dynamic• Fast development as compared to other tools• Portable across databases• Ease of use• High performance• Integration with EJB
  37. 37. JDO Cons• Not “proven”, as in J2EE• Added features by vendors
  38. 38. JDO To Do list• Nested Transactions• Savepoint, Undosavepoint• Inter-PersistenceManager References• Enhancer Invocation API• Prefetch API• BLOB/CLOB datatype support• Managed (inverse) relationship support• Closing PersistenceManagerFactory at VM shutdown• Case-Insensitive Query• String conversion in Query• Read-only fields• Enumeration pattern• Non-static inner classes• Projections in query• LogWriter support
  39. 39. JDO - now and future• Now - maintenance release JDO 1.0.1• Jboss– Then:”JDO will probably go down in history as the proverbialchicken that crossed the road when the CMP2.0 truckcame along”.Marc Fleury, Why I Love EJBs– Almost now: JDO for their CMP implementation– Now: Hibernate?• Sun 2002 ”off”, 2003 ”on”• JDO 2.0 in the works
  40. 40. Resources: Links• Sun• JDO• JSR
  41. 41. Resources: BooksJava Data ObjectsFree pdfwww.ogilviepartners.comJava Data ObjectsCraig Russell JSR 12Core Java Data ObjectsUnderstanding and usingJava Data Objects
  42. 42. JDO vendors• Kodo JDO – SolarMetric• Fast Objects – Poet Software• LiDO – Lebelis• OpenFusion - PrismTech• enJin – Versant• ObjectFrontier - ObjectFrontier
  43. 43. JDO Open Source ”Race”Who will be first?• JORM• TJDO2.0 - 11 November 2003• XORM• OJB• JBossDO– Hibernate (will be JDO 2.0 compliant?)
  44. 44. Enhancer”Add persistent ability” to your domain model.Can use:• Bytecode enhancer• Sourcecode enhancer• ”By hand” - not practical