The following sections demonstrate how to use the generate ORM code with factory method persistent API.
The following code demonstrate how to insert a Product record:
PersistentTransaction t = ErdPersistentManager.instance().getSession().beginTransaction();
try {
Product product = ProductFactory.createProduct();
product.setName("ABC Keyboard");
product.setPrice(24.5);
product.save();
}
catch (Exception e) {
t.rollback();
}
Factory method provides a convinent listByQuery method, accept condition and order by as parameter, and return array of persistent object.
The following code demonstrate how to select a list of Product records, null for condition parameter will select all records, null for order by parameter does not sort in any order:
Product[] products = ProductFactory.listProductByQuery(null, null);
for (int i = 0; i < products.length; i++) {
System.out.println(products[i]);
}
Another useful method to select a persistent object by ID is loadByORMID. The follow code demonstrate how to select a lProuct record by ID.
Product product = ProductFactory.loadProductByORMID(1);
The following code demonstrate how to update a Product record:
PersistentTransaction t = ErdPersistentManager.instance().getSession().beginTransaction();
try {
Product product = ProductFactory.loadProductByORMID(1);
product.setName("DEF Keyboard");
product.save();
}
catch (Exception e) {
t.rollback();
}
The following code demonstrate how to delete a Product record:
PersistentTransaction t = ErdPersistentManager.instance().getSession().beginTransaction();
try {
Product product = ProductFactory.loadProductByORMID(1);
product.delete();
}
catch (Exception e) {
t.rollback();
}