You can use the persistent manager instance to perform the following operations on persistent objects:
· Create an object
· Retrieve a single or more persistent objects
· Update an object
· Delete an object
You can create an object with particular parameters. To do this, you have to:
...
1. Get a persistent manager instance.
2. Start a persistence manager transaction.
3. Instantiate the object.
4. Invoke the makePersistent() method of the persistence manager.
5. Commit the transaction
6. Close the persistence manager.
private void createDepartment(int depId, String name) { PersistenceManager pm = null;
try { pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction();
tx.begin(); Department dep = new Department(depId); dep.setName(name); pm.makePersistent(dep); tx.commit(); } finally { if (pm != null && !pm.isClosed()) pm.close(); } } |
The ID is the primary key field for the Employee and the Department classes, therefore it has to be a unique identifier. You can use it to retrieve a single object from the data store:
Employee emp = (Employee)pm.getObjectById(new Employee.Id(0), false); |
To update an object, you have to:
...
1. Retrieve it from the data store.
2. Change the values of the persistent fields of the JDO instance. The JDO runtime automatically tracks changes on persistent objects and transparently sends the updates back to the data store at commit time.
private void raiseSalary(int empId, double percent) { PersistenceManager pm = null;
try { pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction();
tx.begin();
Employee emp = (Employee)pm.getObjectById(new Employee.Id(empId), false); double oldSalary = emp.getSalary(); double newSalary = oldSalary + percent * oldSalary; emp.setSalary(newSalary);
tx.commit(); } finally { if (pm != null && !pm.isClosed()) pm.close(); } } |
The method raiseSalary() raises the salary of an employee by a given percentage. First, the employee is retrieved from the database with the getObjectById() method. Then the new salary is computed from the old one. The salary of the employee is changed by calling the method setSalary() on the employee with the new salary as the parameter. When the transaction is committed (tx.commit()), the update is sent to the database.
To delete an object, you have to:
...
1. Retrieve it from the data store.
2. Invoke the removePersistent() method of the persistence manager.
private void deleteEmployee(int empId) { PersistenceManager pm = null;
try { pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction();
tx.begin(); Employee emp = (Employee)pm.getObjectById(new Employee.Id(empId), false); Department dep = emp.getDepartment(); dep.removeEmployee(emp); pm.deletePersistent(emp); tx.commit(); } finally { if (pm != null && !pm.isClosed()) pm.close(); } } |
The method deleteEmployee()deletes an employee that is retrieved from the data store with getObjectById(). The relationship between the employees and the department is many-to-one.
Since in your JDO implementation the Java model does not reflect the removal of the object in the database, you have to remove the employee from the employee set of its department manually to keep the Java model consistent with the persistent data.
The extent of a persistent capable class is the collection of all persistent instances of this class in the data store. JDO supports the notion of extent using the javax.jdo.Extent interface. You can consider an instance of javax.jdo.Extent as a handle to a collection of persistent objects in the data store. The persistence manager acts as a factory for extents. In order to loop over all persistent objects of an extent, you have to obtain an iterator from the extent and use the iterator over all objects.
PersistenceManager pm = null;
try { pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); tx.begin(); Extent ext = pm.getExtent(Department.class, false); Iterator it = ext.iterator();
while (it.hasNext()) { Department dep = (Department)it.next(); // Do something with the department }
ext.close(it);
tx.commit(); } finally { if (pm != null && !pm.isClosed()) pm.close(); } } |
In this example, the extent is obtained using the getExtent() method of the persistence manager. The Boolean argument of getExtent determines whether persistent instances of subclasses have to included into the extent or not. You get an iterator by invoking the method iterator() on the extent. If you do not do any transactional work on any element of the iterator – that is, if you do not change persistent objects – you do not need to declare a transaction. Finally, you close the extent and the iterator by calling the close()method on the extent. This enables the JDO implementation to release all the resources it has used during the iteration.