Java Data Object (JDO) Introduction

zhaozj2021-02-16  55

Java Data Object (JDO) Introduction

Byjeff Brown, Senior Software Engineer Object Computing, Inc. (OCI)

Introduction

Java Data Objects (JDO) IS is a specification for the Java object. It has been defined as JSR12 specification by JCP organizations. JDO's first version was submitted on June 6, 2000 and officially issued a 1.0 version specification on April 30, 2002. The two main purposes of the specification are to provide an API of the data processing and access mechanism and the implementation of the allowable specification as part of the application server.

JDO and JDBC

JDBC and JDO are APIs in Java calls the database. The difference is that they have different specific methods for accessing data storage. JDBC provides a very good mechanism that enables application code to be independent of specific database vendors and database locations. In most cases data storage is a relational database. But when you use JDBC drivers to run in a non-relational database, it is not very good. For JDO, the database running under it can be a relational database, object-oriented database or other completely different databases. When JDO is running on the relational database, it can complete data storage through JDBC. And all of this is transparent to developers, all implementations have JDO itself to complete.

JDBC does not support object-oriented database representations. The JDBC database represents completely surrounding the relational database model. This often causes an intermediate layer between the application and the database when writing code. The role of this layer may be an exploded Java object. Decomposing the process of the Java object will change slightly to the object used, so objects can be stored in the relational database. The same must have a mechanism to convert records in the database into appropriate Java objects. JDO is just the opposite of JDBC, and his database is labeled fully objects. This mechanism is not new, this mechanism has been used in some object-oriented databases. JDBC's query language is generally SQL. JDO's query language looks closer to Java. Using JDO No need to learn a query language such as SQL. If you will use Java, you can use JDO query language.

carried out

The JDO specification defines the contents of the JDO implementation of the JDO. Sun provides a reference implementation of a JDO specification, but in May 2002, it does not fully implement the contents required in the norms. There are also many manufacturers available to JDO implementation. A part is listed in the table below.

Manufacturer Products Solarmetric Kodo JDO PrismTech OpenFusion Java Data Objects Signsoft IntelliBo Poet FastObjects

API

Javax.jdo.spi.PersistenceCapable

Any class that is controlled by JDO must expand the PersistenceCapable interface. An instance of any class that extends the PersistenceCapable interface can be seen as a "JDO Instance". This interface defines the method of JDO implementation to control the instance of the class.

Public Abstract Javax.jdo.PersistenceManager JDoGetPersistenceManager ();

Public Abstract Void JdorePlaceStateManager (javax.jdo.spi.StateManager)

Throws securityException;

Public Abstract Void JDOPROVIDEFIELD (INT);

Public Abstract Void JDOPROVIDEFIELDS (INT]);

Public Abstract Void JDOREPLACEFIELD (INT);

Public Abstract Void JDOREPLACEFIELDS (INT []);

Public Abstract void jdoreplaceflags ();

Public Abstract Void JDocopyFields (Object, int []);

Public Abstract Void JdomakeDirty (String); Public Abstract Object JDoGetObjectId ();

Public Abstract Object JDOGETTRANSACTIONALOBJECTID ();

Public Abstract Boolean JDOISDIRTY ();

Public Abstract Boolean JDOISTRANSACTIONAL ();

Public Abstract Boolean JDOISPERSISTENT ();

Public Abstract Boolean JDoisnew ();

Public Abstract Boolean JDoISDELETED ();

Public Abstract Javax.jdo.spi.PersistenceCapable

JDONEWINSTANCE (Javax.jdo.spi.StateManager);

Public Abstract Javax.jdo.spi.PersistenceCapable

JDONEWINSTANCE (Javax.jdo.spi.StateManager, Object);

Public Abstract Object JDONEWOBJECTIDITANCE ();

Public Abstract Object JDONEWOBJECTIDINSTANCE (STRING);

Public Abstract Void JDocopyKeyfieldStoObjectId (Object);

Public Abstract Void JDocopyKeyfieldStoBjectId (

Javax.jdo.spi.PersistenceCapable.ObjectIdfieldsupplier, Object);

Public Abstract Void JDocopyKeyfieldsFromObjectId (

Javax.jdo.spi.PersistenceCapable.ObjectIdfieldConsumer, Object);

There is usually a tool provided by the JDO implementation vendor to complete the transformation of a normal Java class to the JDO instance class. This tool may be implemented in different ways. The first method is to be implemented by bytecode enhancer. A JDO Byte Code Enhancer converts the normal Java class into a "JDO Instance" class is to add the code of the extended PersistenceCapable interface to the class. The second method is to use a code generator that parses the existing normal Java code and then outputs a new version of the new version of the PersistenceCapable interface. If this interface is expanded by manual manual, it is not good.

Javax.jdo.PersistenceManagerFactory

The PersistenceManagerFactory interface is used to get a PersistenceManager instance. Two factory methods are defined in this interface (the content of the factory method can refer to the Java design mode).

Public persistenceManager getPersistenceManager ()

Public PersistenceManager getPersistenceManager (String UserID,

String Password)

Because PersistenceManagerFactory is an interface, some vendor specific class, which implements this interface, must be used as a bootstrap mechanism. This should turn out to be the only vendor specific code that a JDO application uses. Because of this, the JDO specification suggests that an application level factory class be implemented which returns the appropriate instance of the PersistenceManagerFactory so that implementations may be swapped out with minimal impact on application code. Only the application's factory would need to be modified in this case. because PersistenceManagerFactory is an interface, some implementations The manufacturer's specific class of the interface must be used by a "bootstrap" mechanism. // SolarMetric's Example of implementing the PersistenceManagerFactory interface ...

PersistenceManagerFactory ManagerFactory =

New com.solarmetric.kodo.impl.jdbc.jdbcPersistenceManagerFactory ();

// Get a controller Manager ...

PersistenceManager Manager = managerfactory.getPersistenceManager ();

Javax.jdo.PersistenceManager

The PersistenceManager interface is the primary point of contact between a Java application and the JDO implementation. Application code uses a PersistenceManager to retrieve Java objects from the data store and to add Java objects to the data store. The PersistenceManager interface also serves as a factory for The Several Other JDO Components Discussed Below.PersistenceManager interface is the point of connecting the Java application and JDO implementation. The application uses PersistenceManager to get an object from the data store or put a Java object in the data store. The PersistenceManager interface is also serving several JDO components that will be discussed below.

Several methods are defined in the PersistenceManager interface to add the JDO instance object to the data store.

Public Abstract void makepersistent (Object);

Public abstract void makepersistentall (Object []);

Public Abstract void makepersistentall (java.util.collection);

The process of adding a JDO instance object to a data store by the following methods:

// Get a Manager ...

PersistenceManager manager = managerfactory.getPersistenceManager (); // The following Employee must extend PersistenceCapable ...

Employee newemployee = new employee (...);

Manager.makePersistent (newemployee);

Javax.jdo.extent

Extent (range) object represents all the actual class objects in the current database. A factory approach in PersistenceManager is responsible for obtaining an extent (range) object.

Public Extent getExtent (Class Persistencecapableclass, Boolean Subclasses)

The Class parameter indicates the type of object received. The Boolean parameter indicates whether there is a subclass of the class specified by the first parameter.

The Extent interface defines an Iterator () method, which returns a java.util.iterator to traverse all instances described by Extent.

// Get a Manager ...

PersistenceManager Manager = managerfactory.getPersistenceManager ();

// The Employee Class Must Implement PersistenceCapable ...

Extent Employeesextent = Manager.Getextent (Employee.class, False);

Java.util.iterator itrator = Employeesextent.iterator ();

Javax.jdo.query

The QUERY interface allows examples that meet certain conditions from the data store. The Query instance can be obtained from the NewQuery () method from the PersistenceManager interface. The Query interface defines several different versions of the overload execute () method, which will execute the query (query) and return the results of the match.

// Get a Manager ...

PersistenceManager Manager = managerfactory.getPersistenceManager ();

// Employee class must implement the PersistenceCapable interface ...

Extent Employeesextent = Manager.Getextent (Employee.class, False);

// A Query gets all the Employees for more than five years in the company.

Query Query = Manager.newQuery (Employee.class, Employeesextent,

"YearsofemployEment> 5");

// Execute Query ...

Collection Employees = (Collection) query.execute ();

// process result...

Iterator iterator = Employees.ITerator ();

While (item.hasnext ()) {

Employee Employee = (EMPLOYEE) Iterator.next ();

(...)

}

Note the third parameter "YearsofemployEment> 5" of the newquery () method. It indicates the conditions for returning results. The above implementation requires that there must be a property field in the Employee class called YearsofemPloyment.

Example

Introduction

The following example demonstrates the basic application of JDO. The code demonstrates how to add data to the database and retrieve data. All code is used by the SolarMetric's Kodo JDO implementation. Including the mechanism for creating a database, enhances the enhancement of the domain object class extension PersistenceCapable interface and the manufacturer's specific PersistenceManagerFactory implemented KODO JDO implementation. Other parts are used by standard JDO implementations. Domain object

The samples will work with a small set of classes, which represent a Fleet of Vehicle objects. The two specific types of Vehicles defined are Bicycle and MotorVehicle. MotorVehicle objects have an Engine attribute. Examples with a very small class that Describes a Fleet (fleet) of a Vehicle object. Vehicle defines two specific objects BiCyle and MotorveHicle. The MotorVehi object has an Engine property.

Domain object source code

/ **

* Fleet.java

* /

Package com.ociweb.jdodemo;

Import java.util.iterator;

Import java.util.list;

Import java.util.vector;

Public 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 it = getvehicles ();

While (ore.hasnext ()) {

Buffer.Append ("/ t" iter.next () "/ n");

}

Return buffer.toString ();

}

}

/ **

* Vehicle.java

* /

Package com.ociweb.jdodemo;

Public class vehicle {

Private Int Numberofwheels;

Public Vehicle (int Numberofwheels) {

THIS.NUMBEROFWHEELS = NUMBEROFWHEELS;

}

Public int getNumberofwheels () {

Return Numberofwheels;

}

}

/ **

* Bicycle.java

* /

Package com.ociweb.jdodemo;

Public class bicycle extends Vehicle {

PRIVATE STRING Model

Public Bicycle (String Model) {

SUPER (2);

THIS.MODEL = Model;

}

Public string toString () {

Return "Bike: Model" Model

}

}

/ **

* MotorVehi.java

* /

Package com.ociweb.jdodemo;

Public 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;

}

}

/ **

* Engine.java

* /

Package com.ociweb.jdodemo;

Public class engine {

Private Int Numberofcylinders;

Public Engine (int Numberofcylinders) {

THIS.NUMBEROFCYLINDERS = NUMBEROFCYLINDERS;

}

Public int getNumberofcylinders () {

Return Numberofcylinders;

}

Public string toString () {

Return NumberOfcylinders "Cylinder Engine."

}

}

KODO JDO Specification

Kodo JDO contains your own class to create database models and enhanced classes.

Model generation tools are used in the JDO object instances, which will be used to store JDO object instances. The database officially supported database includes:

DB2 Instantdb SQLServer MySQL Oracle PostgreSQL

The JDBC driver of other databases can be added by the extension code. Details Refer to Kodo JDO documentation.

The model creation tool relies on a package.jdo file that is used to define some of the details of the JDO instance class. The following file is used in this example. You can view the documentation of KODO JDO to get details of the file format and concept.

The Schematool.bat file is used to run the model creation tool. The .jdo file must be placed as a parameter in the command line.

Schematool.bat package.jdo

Once the model is created, the class file of the domain object must be enhanced to implement the PersistenceCapable interface. The JDoc.bat file is used to run the class enhancer. The jdoc.bat batch file also needs package.jdo files as parameters to the command line. Jdoc.bat package.jdo

The model creation and class enhancement above is unique to KODO JDO, which is not part of the JDO specification. Other vendors may have their own way to complete the above part, and the specific implementation of the documentation to be referred to.

Add data to the database

Since the database has been configured and our domain objects have been designed, the code also enhances the PersistenceCapable interface, and now those classes can be instantiated and added to the database.

The following class will instantiate a Fleet, add a few data to him and store it into the database.

/ **

* SEEDDATABASE.JAVA

* /

Package com.ociweb.jdodemo;

// manufacturer implemented in PersistenceManagerFactory

Import com.solarmetric.kodo.impl.jdbc.jdbcPersistenceManagerFactory;

Import javax.jdo.PersistenceManager;

Import javax.jdo.transaction;

Public class seeddatabase {

Public static void main (String [] args) {

// Create a Vehicles's Fleet ...

Fleet Fleet = New Fleet ();

Fleet.addVehi (New Bicycle ("Schwinn"));

Fleet.addVehi (New Bicycle ("giant");

Fleet.addvehicle (4, New Engine (8));

Fleet.addVehi (New Motorvehicle (2, New Engine (4)));

Fleet.addvehicle (New Motorvehicle (4, New Engine (4)));

// Get a PersistenceManager ...

PersistenceManager PM =

New JDBCPERSISISTENCEMANAGERFACTORY (). getPersistenceManager ();

// Start a Transaction ...

Transaction Transaction = pm.currenttransaction ();

Transaction.begin ();

// Store Fleet ...

Pm.makePersistent (Fleet);

/ / Submit Transaction ...

Transaction.commit ();

// Close Manager ...

PM.Close ();

}

}

Get data from the database

The following code gets all instances of classes (including subclats) from the database and boots them.

/ **

* Listall.java

* /

Package com.ociweb.jdodemo;

// manufacturer implemented in PersistenceManagerFactory

Import com.solarmetric.kodo.impl.jdbc.jdbcPersistenceManagerFactory;

Import javax.jdo.extent;

Import javax.jdo.PersistenceManager;

Import javax.jdo.PersistenceManagerFactory;

Import javax.jdo.qury;

Import java.util.collection;

Import java.util.iterator; public class listall {

Public static void main (String [] args) {

// Vendor-specific factory implementation ...

PersistenceManagerFactory ManagerFactory =

NEW JDBCPERSISISTENCEMANAGERFACTORY ();

// Get a Manager ...

PersistenceManager Manager =

ManagerFactory.getPersistenceManager ();

Extent ext = manager.getextent (vehicle.class, true);

Query Query = Manager.newQuery (Vehicle.class, EXT, ");

Collection vehicles = (color) query.execute ();

Iterator iterator = vehicles.iterator ();

While (item.hasnext ()) {

Vehicle vehicle = (vehicle) iterator.next ();

System.out.println ("Vehicle =" Vehicle;

}

Manager.close ();

}

}

ListALL output content:

Vehicle = Bike: Model Schwinn

Vehicle = bike: model giant

Vehicle = Motorvehicle with 4 Wheels. 8 Cylinder Engine.

Vehicle = motorvehicle with 2 wheels. 4 Cylinder Engine.

Vehicle = motorvehicle with 4 wheels. 4 Cylinder Engine.

The result of the restriction output is a VEHICLE object of four Cylinders. Conditional parameters must be placed in NewQuery (). Note that this example uses the MotorVechChicle class to instantiate Vechicle because only MotorVehi objects are Engines.

/ **

* Listfourcylindervehicles

* /

Package com.ociweb.jdodemo;

// manufacturer implemented in PersistenceManagerFactory

Import com.solarmetric.kodo.impl.jdbc.jdbcPersistenceManagerFactory;

Import javax.jdo.extent;

Import javax.jdo.PersistenceManager;

Import javax.jdo.PersistenceManagerFactory;

Import javax.jdo.qury;

Import java.util.collection;

Import java.util.iterator;

Public class listfourcylindervehicles {

Public static void main (String [] args) {

// Vendor-specific factory implementation ...

PersistenceManagerFactory ManagerFactory =

NEW JDBCPERSISISTENCEMANAGERFACTORY ();

// Get a manager ...

PersistenceManager Manager =

ManagerFactory.getPersistenceManager ();

Extent ext = manager.getextent (motorvehicle.class, true); // only returns 4 cylinders' VEHICLES ...

Query Query = Manager.newQuery (MotorVehi.class, EXT,

"Engine.Numberofcylinders == 4");

Collection vehicles = (color) query.execute ();

Iterator iterator = vehicles.iterator ();

While (item.hasnext ()) {

Vehicle vehicle = (vehicle) iterator.next ();

System.out.println ("Vehicle =" Vehicle;

}

Manager.close ();

}

}

ListfourcylinderRVEHICLES output is:

Vehicle = motorvehicle with 2 wheels. 4 Cylinder Engine.

Vehicle = motorvehicle with 4 wheels. 4 Cylinder Engine.

in conclusion

JDO provides a database representation that it is better than JDBC. Specific object mappings and database manufacturers are hidden in developers (in other words developers don't have to manage). The database operation of storage and query is very simple. Developers write about data stored code very little. There is reason to believe that JDO is very eye-catching for Java data storage.

reference

JCP - http://www.jcp.org/ jdo - http://access1.sun.com/jdo/ jdo specification - http://www.jcp.org/jsr/detail/12.jsp JSR - http: //www.jcp.org/jsr/overview/index.jsp jdbc - http://java.sun.com/products/jdbc/ jdo central - http://www.jdocentral.com/ Poet FastObjects JDO - HTTP: //www.fastobjects.com/FO_Products_FastObjectsj1_Body.html PrismTech OpenFusion JDO - http://www.prismtechnologies.com/English/Products/JDO/index.html Signsoft IntelliBO JDO - http://www.signsoft.com/en/ IntelliBO / JDO.JSP Solarmetric Kodo JDO - http://www.solarmetric.com/software/kodo_jdo/

转载请注明原文地址:https://www.9cbs.com/read-25145.html

New Post(0)