Java Data Object (JDO) Introduction (4)

xiaoxiao2021-03-06  35

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.

[pre]

[/ pre]

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; // vendor implementations of 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) {// creates a VEET FLEET = New Fleet (); fleet.addvehicle (New Bicycle ("Schwinn")); Fleet.AddVehi (New Bicycle ("giant")); Fleet.Addvehicle (4, New Engine (8))); Fleet.Addvehicle (2, New Engine (4))); Fleet.AddVehi (4) , New Engine (4)))); // Get a PersistenceManager ... PersistenceManager PM = New JDBCPERSISTENCEMANAGERFAACTORY (). 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 in the database

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

/ ** * ListAll.java * / package com.ociweb.jdodemo; // vendor implementations of PersistenceManagerFactory import com.solarmetric.kodo.impl.jdbc.JDBCPersistenceManagerFactory; import javax.jdo.Extent; import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManagerFactory; import javax.jdo.Query; import java.util.Collection; import java.util.Iterator; public class ListAll {public static void main (String [] args) {// vendor-specific implementation of the factory. .. PersistenceManagerFactory managerFactory = new JDBCPersistenceManagerFactory (); // get a manager ... PersistenceManager manager = managerFactory.getPersistenceManager (); Extent ext = manager.getExtent (Vehicle.class, true); Query query = manager.newQuery (Vehicle. Class, EXT, ""); Collection Vehicles = (Collection) Query.execute (); Iterator Iterator = Vehicles.iterator (); while (item.hasnext ()) {vehicle vehicle = (vehicle) item.next (); system.Out.println ("vehicle =" us);} manager.close (); } Listall output content:

[pre] 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. [/ pre]

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; // PersistenceManagerFactory vendor implementations of import com.solarmetric.kodo.impl.jdbc.JDBCPersistenceManagerFactory; import javax.jdo.Extent; import javax.jdo.PersistenceManager; import javax. jdo.PersistenceManagerFactory; import javax.jdo.Query; import java.util.Collection; import java.util.Iterator; public class ListFourCylinderVehicles {public static void main (String [] args) {// ... vendor-specific implementation of the factory PersistenceManagerFactory managerFactory = new JDBCPersistenceManagerFactory (); // get a manager ... PersistenceManager manager = managerFactory.getPersistenceManager (); Extent ext = manager.getExtent (MotorVehicle.class, true); // return only vehicles 4 th of cylinders. .. query query = manager.newquery (Motorvehi.class, EXT, "Engine.Numberofcylinders == 4"); Collection Vehicles = (Collection) query.execute (); iterator itrator = vehicles.iterator (); while (item.hasnext ()) {vehicle us = (vehicle) itemtor.next ); System.out.println ("vehicle =" vehicle);} manager.close ();}} ListfourcyLinderRVEHICLES output results are:

[pre] Vehicle = motors. 4 cylinder engine.

Vehicle = motorvehicle with 4 wheels. 4 cylinder engine. [/ pre]

in conclusion

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

New Post(0)