1
|
- Gopalan Suresh Raj
- Chief Architect
- HYWY Software Corporation
|
2
|
- HYWY’s PE:J Architecture
- The Development Process
- Create UML Model
- Generate Java Source
- Add User’s Business Logic
- JDO Enhance Classes and specify O/R Mapping
- Add Client Code
- O/R Mapping Strategies
- JDO Architecture
- JDO Classes and Interfaces
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
- The Account Class:
- Import Calendar and TimeZone classes to calculate current time
- import java.util.Calendar;
- import java.util.TimeZone;
|
10
|
- The Account Class:
- Initialize the accountType_
field to INVALID_ACCOUNT_ type in the constructor
- public Account() {
- super();
- this.accountType_ =
Account.INVALID_ACCOUNT_;
- }
|
11
|
- The Account class:
- Add logic to the generated create() method
- public int
create(java.lang.String customerNames,
- int
startingBalance)
- throws java.lang.Exception {
- this.customerNames_ =
customerNames;
- this.balance_ =
startingBalance;
- this.openedOn_=
Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime();
- return this.balance_;
- }
|
12
|
- The Account Class:
- Add logic to the credit() and debit() methods
- public int credit(int amount)
throws java.lang.Exception {
- this.balance_ += amount;
- return this.balance_;
- }
- public int debit(int amount)
throws java.lang.Exception {
- this.balance_ -= amount;
- return this.balance_;
- }
|
13
|
- The Checking Class:
- Modify constructor and toString() methods:
- public Checking() {
- super();
- this.accountType_ =
Account.CHECKING_ACCOUNT_;
- }
- public java.lang.String toString() {
- return super.toString();
- }
|
14
|
- The Savings class:
- Modify constructor
- public Savings() {
- super();
- this.accountType_ =
Account.SAVINGS_ACCOUNT_;
- }
- Add logic to calculateInvestments() method
- public int calculateInterest() {
- return this.balance_ *
this.interestRate_/100;
- }
|
15
|
|
16
|
- The JDO Model distinguishes between Persistence-Capable,
Persistence-Aware, and Transient classes
- First Class Objects (FCOs) have a JDO identity
- Second Class Objects (SCOs) do not have a JDO identity
|
17
|
- First Class Objects (FCOs)
- Exist with an Object Identity in the DataStore
- Can be referenced by multiple objects
- Can be Queried
- Second Class Objects (SCOs)
- Is stored as part of a First Class Object (are managed as a field)
- They have no Object Identity of their own
- Change tracking occurs transparently through their owning FCO.
|
18
|
- Primitive types
- References to other PC classes
- References to String, BigDecimal, BigInteger, Locale
- References to Date, Time, TimeStamp
- References to Collection types
- Mandatory: HashSet, Collection, and Set
- Optional: Hashtable, Vector, List, ArrayList, LinkedList, TreeList,
Map, HashMap, TreeMap.
|
19
|
- JDO uses a metadata file in XML format to get additional information
about the class that is enhanced
- Field modifiers in the JDO XML Metadata file override the JDO defaults
- Fields can either be Persistent, Transactional, or Transient.
|
20
|
- <?xml version="1.0" encoding="UTF-8" ?>
- <jdo>
- <package
name="com.hywy.samples.bank">
- <class
identity-type="datastore" name="Account">
- <extension
key="isSCO" value="false"
vendor-name="HYWY" />
- <field default-fetch-group="true"
name="accountType_"
persistence-modifier="persistent" />
- <field default-fetch-group="true"
name="customerNames_"
persistence-modifier="persistent" />
- <field default-fetch-group="true"
name="balance_" persistence-modifier="persistent"
/>
- <field default-fetch-group="true"
name="openedOn_" persistence-modifier="persistent"
/>
- </class>
- </package>
- </jdo>
|
21
|
|
22
|
|
23
|
|
24
|
- Queries with JDO Query Language
- Transactions with begin, commit, and rollback
- Connection Management with pooling and caching services
- Transparent Persistence
- Change tracking and automatic enlistment
- Persistence By Reachability
- Lazy data loading
- Automatic navigation
- Insert and delete handling
|
25
|
- PersistenceManagerFactory (PMF)
- PersistenceManager (PM)
- PersistenceCapable (PC)
- StateManager (SM)
- JDOHelper: This class defines static methods that allow a JDO-aware
application to examine the runtime state of instances
- JDOImplHelper: Contains methods to register metadata for
persistence-capable classes, and for operations that need to be
performed by JDO implementations, not by end-users.
|
26
|
- Every JDO instance implements the PersistenceCapable interface
- Each JDO instance is associated with a Persistence Manager
- A JDO instance can live in both managed and non-managed environments.
- Persistence Managers and underlying connections can be pooled.
- JDO provides Object Caching.
|
27
|
- The Teller Class:
- Import the PE:J Adapter and Oid Server class
- import com.hywy.pej.adapter.JDOAdapter;
- import com.hywy.pej.persistence.oidserver.Oid;
- Import the standard JDO library Classes
- import javax.jdo.PersistenceManagerFactory;
- import javax.jdo.PersistenceManager;
- import javax.jdo.Transaction;
- import javax.jdo.Extent;
- import javax.jdo.spi.PersistenceCapable;
|
28
|
- The Teller Class:
- /** Project Name that was configured using the Console */
- public static final String
PROJECT_NAME_ = "bank";
- /** Datastore Name that was
configured using the Console */
- public static final String
DATASTORE_NAME_= "bank_datastore";
- /** PMF Object pool initial
capacity */
- public static final int
INITIAL_POOL_CAPACITY_ = 1;
- /** PMF Object pool maximum
capacity */
- public static final int
MAXIMUM_POOL_CAPACITY_ = 10;
- /** PMF Object pool capacity
increment */
- public static final int
POOL_CAPACITY_INCREMENT_ = 1;
|
29
|
- The Teller Class: Retrieving a configured PMF
- public Teller() {
- this.adapter_ = new
JDOAdapter(Teller.PROJECT_NAME_,
-
Teller.INITIAL_POOL_CAPACITY_,
-
Teller.MAXIMUM_POOL_CAPACITY_,
-
Teller.POOL_CAPACITY_INCREMENT_);
- this.pmFactory_ =
this.getAdapter().obtainPMF(Teller.PROJECT_NAME_,
-
Teller.DATASTORE_NAME_);
- }
|
30
|
- Helps Configure transaction options
- Helps get connection factory
- Helps configure JDBC settings
- Holds Persistence Manager pools
- Helps obtain a PM
- This class is Serializable
|
31
|
- Pools pre-configured Persistence Manager Factories (PMFs)
- Pre-configures datastores with user and project information
- Works for both managed and non-managed environments
|
32
|
- The Teller Class: Add a createAccount() method
- public void createAccount(String customerNames, int startingBalance) {
- Account account = new Checking();
- account.create(customerNames,
startingBalance);
- PersistenceManager pManager =
null;
- Transaction transaction =
null;
- try {
- pManager =
this.pmFactory_.getPersistenceManager();
- transaction =
pManager.currentTransaction();
- transaction.begin();
-
pManager.makePersistent(account);
- transaction.commit();
- } catch(Exception exception) {
- transaction.rollback();
- }
- }
|
33
|
- Primary interface for JDO operations
- Provides a logical connection to the datastore
- Provides a query factory
- Provides an Extent factory
- Provides Cache Management
- Provides Extent Management
- Provides JDO Identity Management
- Provides Instance Life-cycle management
|
34
|
- Some of the methods available in the PM are:
- Transaction currentTransaction();
- Query newQuery();
- Extent getExtent(Class pcClass, boolean subclasses);
- Object getTransactionalObjectId(Object pc);
- void makePersistent(Object pc);
- void makePersistentAll(Object[] pcs);
- void deletePersistent(Object pc);
- void deletePersistent(Object[] pcs);
- void makeTransient(Object pc);
- void makeTransient(Object[] pcs);
- void makeTransactional(Object pc);
- void makeTransactional(Object[] pcs);
- void evict(Object pc);
- void evictAll(Collection pcs);
|
35
|
- Datastore (Pessimistic) Transaction Management
- Optimistic Transaction Management
- In non-managed environments, a local transaction is associated with the
PM
- In managed environments, a user transaction, or a local transaction is
associated with the PM
- Optimistic Transaction support is optional
- PE:J provides both Optimistic and Datastore (Pessimistic) Transaction
Management
|
36
|
- Some of the methods of the Transaction interface are:
- void begin();
- void commit();
- void rollback();
- boolean isActive();
- void setOptimistic(boolean flag);
- void setNontransactionalRead(boolean flag);
- void setNontransactionalWrite(boolean flag);
- void setRetainValues(boolean flag);
- void setSynchronization(Synchronization sync);
|
37
|
- This method makes the transient instance persistent
- This method must be called in an active transaction.
- The PersistenceManager assigns an ObjectId to the instance and
transitions it to persistent-new.
- The instance will be managed in the Extent associated with its Class.
- The instance will be put into the data store at commit.
- The closure of instances of PersistenceCapable classes reachable from
persistent fields will be made persistent at commit. This is known as
“Persistence by Reach ability”.
|
38
|
- The Teller Class - Add a transferMoney() method:
- public void transferMoney(long fromAccountNo, long toAccountNo, int
amount) {
- Transaction transaction =
null;
- try {
- PersistenceManager pManager
= this.pmFactory_.getPersistenceManager();
- transaction =
pManager.currentTransaction();
- transaction.begin();
- Account
fromAccount=(Account)pManager.getObjectById(new Oid(fromAccountNo),
-
true);
- Account toAccount =
(Account)pManager.getObjectById(new Oid(toAccountNo),
-
true);
- fromAccount.debit(amount);
- toAccount.credit(amount);
- transaction.commit();
- } catch(Exception exception) {
- transaction.rollback();
- }
- }
|
39
|
- Locates a persistent instance in the cache of instances managed by the
Persistence Manager.
- The getObjectById method attempts to find an instance in the cache with
the specified JDO identity.
- If it is not available in the cache, it is loaded from the datastore.
|
40
|
- The Teller Class – Add listAllAccounts()
- public void
listAllCheckingAccounts() {
- try {
- PersistenceManager pManager
= pmFactory_.getPersistenceManager();
- Class pcClass =
Class.forName("com.hywy.samples.bank.Checking");
- Extent extent =
pManager.getExtent(pcClass, false) ;
- Iterator iterator =
extent.iterator();
- while (iterator.hasNext()) {
- Checking checking =
(PersistenceCapable)iterator.next();
- Oid oid =
(Oid)pManager.getObjectId(checking);
-
System.out.println(“Checking Account Number:”+oid.toString());
- }
- } catch(Exception exception) {
-
exception.printStackTrace();
- }
- }
|
41
|
- Instances of the Extent class represent the entire collection of
instances in the data store of the candidate class possibly including
its subclasses.
- The Extent instance has two possible uses:
- To iterate all instances of a particular class
- To execute a Query in the data store over all instances of a particular
class
|
42
|
- The getObjectId method returns an ObjectId instance that represents the
object identity of the specified JDO instance.
- The identity is guaranteed to be unique only for two types of JDO
Identity: those that are managed by the application, and those that are
managed by the data store.
|
43
|
- The Teller Class – Add deleteAccount()
- public void deleteAccount(long
accountNumber) {
- Account account = null;
- PersistenceManager pManager =
null;
- Transaction transaction =
null;
- try {
- pManager =
this.pmFactory_.getPersistenceManager();
- transaction =
pManager.currentTransaction();
- transaction.begin();
-
account=(Account)pManager.getObjectById(new Oid(accountNumber),
-
true);
-
pManager.deletePersistent(account);
- transaction.commit();
- } catch(Exception exception) {
- transaction.rollback();
- }
- }
|
44
|
- Delete the persistent instance from the data store.
- This method must be called in an active transaction.
- The data store object will be removed at commit.
- Unlike makePersistent, which makes the closure of the instance
persistent, the closure of the instance is not deleted from the data
store.
- This method has no effect if the instance is already deleted in the
current transaction.
|
45
|
- Compile the Teller Class
- Run the JDO Client Application
|
46
|
- Sun’s JDO Resources Site
- http://access1.sun.com/jdo/
- HYWY’s Website to download PE:J
- http://www.hywy.com/
- Gopalan Suresh Raj’s PE:J resources
- https://pejava.tripod.com/
- Gopalan Suresh Raj’s JDO resources
- http://www.execpc.com/~gopalan/java/jdo/jdo.html
|