PersistenceManager
PersistenceManager can be said to be a very frequent class in JDO. Many of the operations of persistent objects (that is, the operation of the database) is conducted through this class. Today I put a simple explanation of the main methods in this class, I hope to help you learn JDO.
The above figure shows the common methods and properties of the PersistenceManager interface, which contains many standard JavaBeaN property methods setter and getter, where the method of the method contains many omitted numbers Description They have a variety of overloaded implementations, the complete PersistenceManager interface can Refer to JAVADOC in JDO.
The PersistenceManager interface is the primary interface that the developer exchanges in the application with JDO. Each PersistenceManager interface controls a series of persistent objects and transmission objects and associated a unique transaction object.
The method of the Persistencemanager interface can be divided into the following parts:
l Association of user objects
l Configure properties
l Transaction
l Control of the persistent life cycle
l JDO consistency control
l Query Factory
l extent factory
l Close method
1, user object association
Public Object GetUserObject ();
Public void setuserObject (Object obj);
The USEROBJECT of the PersistenceManager allows any object to be associated with each PersistenceManager. This object will not be used by JDO.
2, configuration properties
Public Boolean getMultithreaded ();
Public void setmultithreaded (Boolean Threaded);
Public boolean getignorecache ();
Public void setignorecache (Boolean Ignore);
PersistenceManager has two configuration properties: MultithReaded and Ignore. Set whether it can be shared and ignored whether the state of the persistent object state is ignored, and the specific details have been taught in the previous section, which is not repeated here.
3, Transaction Association
Public Transaction CurrentTransaction ();
Every PersistenceManager corresponds to a unique Transaction object. In fact, many vendors use a class to implement the PersistenceManager and Transaction interfaces. If you need multiple Transaction objects, you need multiple PersistenceManager. You can get the TRANSACTION of the current PersistenceManager through the above way.
4. Control of the sustainable life cycle
PersistenceManager controls the life cycle of persistent objects through several actions. Each action has multiple methods to achieve. For example, makepersistent receives a persistent object, while MakePersistentall receives a collection of persistent objects.
Public Void MakePersistent (Object PC);
Public Void MakePersistentall (Collection PCS);
Public void makepersistentall (Object [] PCS);
The above method is used for a process of processing, and the new data is written to the database when a processing process is submitted.
Public Void DeletePersistent (Object PC);
Public void deletePersistentall (Collection PCS); Public void deletePersistentall (Object [] PCS);
The above method is called when the persistent object is deleted.
Public Void MakeTransient (Object PC);
Public Void MakeTransientAll (Collection PCS);
Public void maketransientall (Object [] PCS);
The above method converts the persistent object to an instantaneous object so that this object will associate with the PersistenceManager and JDO consistency, so the change will not affect the database.
Public Void MakeTransactional (Object PC);
Public Void MakeTransactionAlal (Collection PCS);
Public Void MakeTransactionAll (Object [] PCS);
The above method can put non-persistent objects in the current processing session for processing.
Public void Makenontransactional (Object PC);
Public Void MakenontransactionAlalL (Collection PCS);
Public void MakenontransactionAlall (Object [] PCS);
Conversion of instantaneous clear objects into instantaneous objects, converting persistently clear objects into persistent non-processing objects. If you call this method on a "torque" object to throw a JDOUSEREXCEPTION exception.
Public void evic (Object PC);
Public Void EvicTall (Collection PCS);
Public void evicTall (Object [] PCS);
Public void evicTall ();
Taken out an object, telling the PersistenceManager application that no longer needs this object.
Public Void Refresh (Object PC);
Public Void Refreshall (Collection PCS);
Public void refreshall (Object [] PCS);
Public void refreshall ();
Keep your persistence object synchronize with data in the database using the above method.
Public void Retrieve (Object PC);
Public void Retrievell (Collection PCS);
Public void retrieveall (Object [] PCS);
Public void Retrievell (Collection PCS, Boolean Dfgonly);
Public void retrieveall (Object [] PCS, Boolean Dfgonly;
Get the data value of the persistent object from the database.
5, an example of a life cycle
1) Treatment object
// Create an object
Magazine MAG = New Magazine ("1B78-YU9L", "JavaWorld");
Company Pub = New Company ("Weston House");
Pub.setRevenue (1750000D);
Mag.SetPublisher (PUB);
Pub.Addmagazine (MAG);
Article Art = New Article ("JDO Rules!", "Transparent Object Persistence");
Art.SetAuthor (New Person ("Fred", "Hoyle"); Mag.Addarticle (ART);
/ / Get the PersistenceManager object
PersistenceManager PM = PMFactory.getPersistenceManager ();
Pm.currentTransaction () .begin ();
Pm.makePersistent (MAG);
PM.currentTransaction () .commit ();
// Close the PersistenceManager object
PM.Close ();
2) Update object
/ / Suppose we have gained an object via GetObjectID
Object Oid = ...
// Create a PersistenceManager and get Mahazine objects
PersistenceManager PM = PMFactory.getPersistenceManager ();
Magazine MAG = (Magazine) PM.GetObjectByid (OID, FALSE);
Company Pub = MAG.GETPUBLISHER ();
/ / Update the data and submit to the database
Pm.currentTransaction () .begin ();
Mag.setissue (23);
Company.SetRevenue (1750000D);
PM.currentTransaction () .commit ();
// Turn off the PersistenceManager object and release the resource.
PM.Close ();
3) Delete the object
/ / Suppose we have gained an object via GetObjectID
Object Oid = ...
// Create a PersistenceManager and get Mahazine objects
PersistenceManager PM = PMFactory.getPersistenceManager ();
Magazine MAG = (Magazine) PM.GetObjectByid (OID, FALSE);
/ / Delete the data in the object and submit to the database
Pm.currentTransaction () .begin ();
Pm.DeletePersistentall (MAG.GetArticles ());
PM.currentTransaction () .commit ();
// Turn off the PersistenceManager object and release the resource.
PM.Close ();
6, JDO consistency control
Each PersistenceManager is responsible for maintaining the JDO consistency of current persistent objects. The following code allows you to control the consistency of JDO:
Public Class GetObjectIdclass (Class Pcclass);
The following method obtains a persistent object through GetObjectID and maintains JDO consistency based on a given consistency field field.
Public Object GetObjectId (Object PC);
Public Object NewObjectIdIntInstance (Class Pcclass, String IdentityString);
7, Extent factory
Public Extent GetExtent (Class Pcclass, Boolean IncludesubClasses);
Extent is a logical representation of a given persistent object instance, which can be obtained by the getExtent () method of the PersistenceManager. This method has two parameters: the first is a persistence class, the second is a boolean value represents a subclass of this persistence class.
If the REQIRES-EXTENT of the persistent class in this method, the requires-extent is false then you will not be able to get this persistent class's extent. 8, Query Factory
Public query newquery ();
Public Query NewQuery (Class CandidateClass);
Public Query NewQuery (Extent Candidates);
Public Query NewQuery (Class CandidateClass, Collection Candidates);
Public Query NewQuery (Class CandidateClass, String Filter);
Public Query NewQuery (Class CandidateClass, Collection Candidates, String Filter);
Public Query NewQuery (Extent Candidates, String Filter);
Public Query NewQuery (String Language, Object Serialized);
Public Query NewQuery (Object Serialized);
Query object is used to query the data from the persistent object, you can get the Query object through a series of Newqery methods of PersistenceManager, and I will tell the use of the Query object.
9, turn off method
Public boolean isclosed ();
Public void close ();
When a PersistenceManager object is no longer in use, you should call it to close it, which releases the resources of this PersistenceManager object. This is the other objects controlled by this PersistenceManager, and if you call this PersistenceManager method (except for isclosed) or throw a JDOUSEREXCEPTION exception.
Copyright Notice:
All articles I wrote about JDO can be reproduced any, but please keep the author information in the reprinted article, thank you.
Author: Liu Guodong
Mail: WAFD@hotmail.com
QQ: 282099538