javax.jdo.PersistenceManagerFactory
PersistenceManagerFactory接口 被用来获得一个PersistenceManager 实例。这个接口中定义两个工厂方法(有关工厂方法的内容可以参考Java设计模式)。
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.
因为PersistenceManagerFactory 是一个接口,一些实现该接口的厂商特定的类必须通过一种“自举”机制来使用。
// SolarMetric's 实现PersistenceManagerFactory 接口的例子...
PersistenceManagerFactory managerFactory =
new com.solarmetric.kodo.impl.jdbc.JDBCPersistenceManagerFactory();
// 获得一个控制器 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 several other JDO components discussed below.
PersistenceManager 接口是连接Java应用程序和JDO实现的要点。应用程序使用PersistenceManager 从数据存储中获得对象或者把一个Java对象放到数据存储中。PersistenceManager 接口也为下面将要讨论的几个JDO组件提供服务。
PersistenceManager 接口中定义了几个方法来把JDO实例对象添加到数据存储中。
public abstract void makePersistent(Object);
public abstract void makePersistentAll(Object[]);
public abstract void makePersistentAll(java.util.Collection);
通过下面几个方法实现把JDO实例对象添加到数据存储中的过程:
// 获得一个 manager...
PersistenceManager manager = managerFactory.getPersistenceManager();
// 下面的 Employee 必须扩展了 PersistenceCapable...
Employee newEmployee = new Employee(...);
manager.makePersistent(newEmployee);
javax.jdo.Extent
Extent (范围)对象表示所有的在当前数据库中实际的类对象。在PersistenceManager 中的一个工厂方法负责获得一个Extent (范围)对象。
public Extent getExtent(Class persistenceCapableClass, boolean subclasses)
Class参数标明了接收的对象类型。boolean 参数标明是否包含第一个参数指定的类的子类。
Extent 接口定义了一个iterator()方法,它返回一个 java.util.Iterator来遍历所有由Extent 描述的实例。
//获得一个 manager...
PersistenceManager manager = managerFactory.getPersistenceManager();
// the Employee class must implement PersistenceCapable...
Extent employeesExtent = manager.getExtent(Employee.class, false);
java.util.Iterator iterator = employeesExtent.iterator();
javax.jdo.Query
Query 接口允许从数据存储中获得符合某些条件的实例。Query 实例可以重载自PersistenceManager 接口中的newQuery()方法获得。
Query 接口定义了几个不同版本的重载execute() 的方法,这些方法将执行查询(Query) 并返回匹配的结果。
// 获得一个 manager...
PersistenceManager manager = managerFactory.getPersistenceManager();
// Employee 类必须实现 PersistenceCapable接口...
Extent employeesExtent = manager.getExtent(Employee.class, false);
// 一个Query 获得所有在公司工作五年以上的 Employees....
Query query = manager.newQuery(Employee.class, employeesExtent,
"yearsOfEmployement > 5");
// 执行查询Query...
Collection employees = (Collection) query.execute();
// 处理结果...
Iterator iterator = employees.iterator();
while (iterator.hasNext())
{
Employee employee = (Employee) iterator.next();
(...)
}
注意newQuery()方法的第三个参数"yearsOfEmployement > 5"。它标明了返回结果的条件。上面的实现要求Employee 类中必须有一个属性字段叫yearsOfEmployment 。