java data object che-rung lee. jdo objectives transparent persistence range of implementations ...
Post on 22-Dec-2015
227 views
TRANSCRIPT
![Page 1: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/1.jpg)
Java Data Object
Che-Rung Lee
![Page 2: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/2.jpg)
JDO Objectives
Transparent persistence
Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE, EJB)
Datastore independence relational, object, hierarchical databases XML DB, file systems
![Page 3: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/3.jpg)
What’s the difference?
Serialization no database capabilities (transactions, queries)
JDBC cannot storing Java object models. incompatibilities among SQL implementations
can result in a loss of application portability CMP (Container Managed Persistence)
a distributed model of computation, imposing performance degradations
![Page 4: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/4.jpg)
JDO Introduction
JDO APIAn exampleDatastore mappingJDO implementation
![Page 5: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/5.jpg)
JDO API
Two classes I18NHelper JDOHelper
Six interfaces1. PersistenceManagerFactory2. PersistenceManager3. Transaction4. Extent5. Query6. InstanceCallbacks
Persistence Manager Factory
Persistence Manager
Transaction Query Extent
InstanceCallbacks
JDOHelperI18NHelper
![Page 6: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/6.jpg)
1. PersistenceManagerFactory
Create PersistenceManager May implement PersistenceManager
pooling, connection pooling among PersistenceManagers
Datastore configuration Supports JNDI
Persistence Manager Factory
Persistence Manager
Transaction Query Extent
InstanceCallbacks
JDOHelperI18NHelper
![Page 7: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/7.jpg)
2. PersistenceManager
Primary application interface
PersistenceCapable instance management identity life cycle
Transaction factory
Query factory
Extent factory
Persistence Manager Factory
Persistence Manager
Transaction Query Extent
InstanceCallbacks
JDOHelperI18NHelper
![Page 8: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/8.jpg)
3. Transactions
One-one relation to Persistence Manager
Transaction interface for local transactions isActive, begin,
commit, rollback
Provide ACID transaction Atomic, Consistent,
Isolated, Durable
Persistence Manager Factory
Persistence Manager
Transaction Query Extent
InstanceCallbacks
JDOHelperI18NHelper
![Page 9: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/9.jpg)
4. Extents
Defined for PersistenceCapable classes pm.getExtent (Class pc, boolean subclasses);
Used in Query andclass navigation Persistence
Manager Factory
Persistence Manager
Transaction Query Extent
InstanceCallbacks
JDOHelperI18NHelper
![Page 10: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/10.jpg)
5. Query
Can do filtering and ordering
Outer/inner join
Has JDOQL
Persistence Manager Factory
Persistence Manager
Transaction Query Extent
InstanceCallbacks
JDOHelperI18NHelper
![Page 11: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/11.jpg)
6. InstanceCallbacks
Event trigger functions in DBMSPersistenceCapable class that provides
callback methods implements this interfaceMethods
jdoPostLoad(), jdoPreStore(), jdoPreClear(), jdoPreDelete()
Persistence Manager Factory
Persistence Manager
Transaction Query Extent
InstanceCallbacks
JDOHelperI18NHelper
![Page 12: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/12.jpg)
How to use JDO
Write persistent object (Persistence Capable)1. Write normal java objects2. Specify their relation in a XML file3. Enhance classes for persistence
Make transactions (Persistence Aware) Create and connect to a datastore Insert / update / delete Query
![Page 13: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/13.jpg)
Write PersistenceCapable objects
.java
.class
.class
Java Compiler
JDOEnhancer
JDO MetaData(XML)
Byte code enhancement
![Page 14: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/14.jpg)
Address book example
Persistent Object Model Many to many relation
Person Category* *
![Page 15: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/15.jpg)
Classes to Persist
public class Person { Vector category; // element is Category
…public Person() {}
} public class Category { Vector person; // element is Person
…private Category() {}
}
![Page 16: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/16.jpg)
JDO MetaData
<?xml version=“1.0” encoding=UTF-8?><!doctype jdo public http://java.sun.com/dtd/jdo_1_0.dtd><jdo>
<package name=“addressBook”><class name = “Person”> <field name= category>
<collection element-type = “Category”> </field></class><class name = “Category”> <field name= person>
<collection element-type = “Person”> </field></class>
</package></jdo>
![Page 17: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/17.jpg)
JDO Enhancer
Enhance classes to implement the interface PersistenceCapable
run Enhancer with JDO metadata (.xml file) and class files $enhancer$ addressbook.jdo Person.class Cate
gory.class
![Page 18: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/18.jpg)
JDO Runtime Environment
JVMApplication
PersistenceManager
PersistenceCapable
transient
transient
transientPersistenceCapable
PersistenceCapable
Query
Transaction
Extent
![Page 19: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/19.jpg)
Access PersistenceCapable objects
Code template1. PersistenceManagerFactory pmf = JDOHelper.
getPersistenceManagerFactory(properties);2. PersistenceManager pm =
pmf.getPersistenceManager();3. Transaction tx = pm.currentTransaction();4. tx.begin();5. execute(); // transaction code6. tx.commit();7. pm.close();
![Page 20: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/20.jpg)
Create a Datastore
JDO will create database, tables, indexes
Set property ConnectCreate = true Unset this property if not creating a datastore
Do nothing on code void execute() {}
![Page 21: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/21.jpg)
Create persistence object
void execute(){
Person p = new Person(…);
pm.makePersistent(p);
// pm is a PersistenceManager
}
![Page 22: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/22.jpg)
Update persistence object
We need get persistent objects from datastore for update and delete
Assume we already got the object (Person p) from datastore. For update, it’s simple void execute(){
p.setName (newName);
}
![Page 23: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/23.jpg)
Delete persistence object
Assume we get the object (Person p) from datastore, and we want to delete it void execute(){
Vector cat = p.getCategory();
for ( i=0;i<cat.size(); i++) {
Category c = (Category)cat.elementAt(i);
c.getPerson().remove (p);
}
pm.deletePersistent(p);
}
![Page 24: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/24.jpg)
Get objects from datastore
Navigation
Query
From PersistenceManager Object getObjectById(Object oid) Object getTransactionalInstance(Object pco)
![Page 25: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/25.jpg)
Navigate whole class
void execute(){
Extent ext = pm.getExtent(Person.class);
Iterator itor = ext.iterator();
while (itor.hasNext()) {
Person p = (Person) itor.next();
}
}
![Page 26: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/26.jpg)
Query with Filters
void execute(){
Query qry = pm.newQuery(Person.class);
qry.setFilter(filters); // filter is a String
qry.setOrdering(“name ascending; birth descending”);
Collection result = (Collection) qry.execute();
for(Iterator i = result.iterator();i.hasNext();)
Person p = (Person) i.next();
}
![Page 27: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/27.jpg)
Query examples
Find the person named “Roger” qry.setFilter (“name == \”Roger\”“);
Find the person by parameter (String who) qry.declareParameters (“String who");
qry.setFilter (“name == who");
result = qry.execute (who)
![Page 28: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/28.jpg)
JDO Query Language
JDO defines a set of functions for querying isEmpty(): null singleton or collection or empty
collection. contains(): collection startsWith(), endWith(): String matching
Find persons in “CS*” Category qry.declareParameters (“Category cat");
qry.setFilter(“category.contains(cat) && cat.name.startWith(\”CS\”)”);
![Page 29: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/29.jpg)
Inside JDO
PersistenceCapable Object identity Object life cycle
Data mapping Inheritance Collection
Other issues
![Page 30: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/30.jpg)
Primary key of PersistenceCapable
In datastore assigned by the datastore and is not based on
field values in the object
In application uses values of object state to form a unique
identity
Nondurable not uniquely identifiable
![Page 31: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/31.jpg)
How to compare o1 and o2?
Java identity implemented by JVM o1 == o2
Object equality implemented by class developer o1.equals (o2)
JDO identity implemented by JDO vendor o1.jdoGetObjectId().equals(o2.jdoGetObjectId())
![Page 32: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/32.jpg)
Life Cycle of PersistenceCapable
![Page 33: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/33.jpg)
Life Cycle States (1)
Persistent-new. During the current transaction, the instance has
been placed under the control of JDO and a JDO identity has been assigned.
Persistent-clean. The instance’s fields have been read but not
modified in the current transaction. Persistent-dirty.
The instance’s fields have been modified in the current transaction.
![Page 34: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/34.jpg)
Life Cycle States (2)
Hollow. Only the JDO identity is loaded. Non-key fields
are reread from the datastore in subsequent transactions.
Provides uniqueness of persistent instances across transactions.
Hollow objects are subject to garbage collection if the application does not hold strong references to them.
Persistent-clean, persistent-dirty and persistent-new instances all become hollow after commit.
![Page 35: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/35.jpg)
Life Cycle States (3)
Persistent-deleted. Deleted in the current transaction. Access to
non-key fields will throw a JDOUserException. Persistent-new-deleted.
Made persistent-new and then deleted in the current transaction.
Transient, Transient-clean, Transient-dirty The instance has not been placed under the
control of JDO.
![Page 36: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/36.jpg)
Data Mapping
Issues Database normalization: multi-table per class… Performance: number of joins, update… Field mapping: data types, read only… Relation: delete policy…
Implementation dependence Some implementations can configure data
mapping in JDO meta file
![Page 37: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/37.jpg)
Data Mapping
Inheritance Map all hierarchy to base class Discriminator is required
Collection Collection objects are second order object
Not have a JDO identity Its owner can aware of its change
Difference among Set, List and Map
![Page 38: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/38.jpg)
Other issues
Object instantiation during query execution Invocation of object methods
Persistence by reachability Garbage collector in database
JDOQL to SQL compilation Allowing SQL in JDOQL
Cache management Search caches for uncommitted objects
More …
![Page 39: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/39.jpg)
Some JDO Implementations
Free Sun Reference Implementation Object Relational Bridge (OJB)
Commercial Fast Objects Frontier Suit for JDO (Object Frontier) JDO Genie LIBeLIS LiDO SolarMetric Kodo JDO
![Page 40: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/40.jpg)
Next version ?
Managed relationship support Inter-PersistenceManager references JDOQL to support projections API for specification of pre-read policy Enhancer invocation API Read-only fields Generation of sequence numbers for fields. Aggregation functions for JDOQL
![Page 41: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/41.jpg)
Summery
![Page 42: Java Data Object Che-Rung Lee. JDO Objectives Transparent persistence Range of implementations embedded (J2ME) two tier (J2SE) enterprise (J2EE,](https://reader035.vdocuments.us/reader035/viewer/2022062516/56649d7f5503460f94a6349a/html5/thumbnails/42.jpg)
References
http://access1.sun.com/jdo/ http://www.JDOcentral.com/index.html http://www.oreilly.com/catalog/jvadtaobj/chapter/c
h01.pdf
http://www.cognitivemachines.com/JDOchat.html http://java.sun.com/products/jdo/ http://db.apache.org/ojb/ http://servlet.java.sun.com/javaone/javaone2000