Java Reference
In-Depth Information
try {
pm.makePersistent(o);
} finally {
pm.close();
}
Fetching Entities
You can fetch an entity with its key by using the PersistenceManager's
getObjectById
method.
PersistenceManager pm = PMF.get().getPersistenceManager();
Key key = KeyFactory.createKey(Order.class.getSimpleName(),
"
jeff@noemail.co
m");
Order o = pm.getObjectById(Order.class, key);
If you are using an encoded string ID or a numeric ID, you can fetch the entity by
passing the
getObjectById
method the simple value of the key.
PersistenceManager pm = PMF.get().getPersistenceManager();
Order o = pm.getObjectById(Order.class, "
jeff@noemail.co
m");
Updating Entities
You typically update an entity by fetching it with the
PersistenceManager
, make any
changes to the instance, and then close the
PersistenceManager
. When the
PersistenceManager
is closed, it automatically updates any changes to the entity in the
datastore, as the instance is said to be "attached" to the
PersistenceManager
.
public void updateOrder(Order order, String customerName) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Order o = pm.getObjectById(Order.class, order.getId());
o.setName(customerName);
} finally {
pm.close();
}
}