sormula | simple (easy) object relational mapping | |
|
// perform within transaction for the default active database ActiveTransaction transaction = new ActiveTransaction(); try { transaction.begin(); // get part by primary key Inventory inventory = Inventory.table.select("1234"); // update inventory.setQuantity(inventory.getQuantity() - 1); inventory.update(); transaction.commit(); } catch (ActiveException e) { transaction.rollback(); e.printStackTrace(); } // perform within transaction for an active database ActiveTransaction transaction = new ActiveTransaction( new ActiveDatabase(dataSource)); try { transaction.begin(); // select for a specific manufacturer // "manf" is name of where annotation in Inventory.java List<Inventory> list = Inventory.table.selectAllWhere("manf", "ACME"); // for all inventory of manufacturer for (Inventory inventory: list) { inventory.setQuantity(0); } // update Inventory.table.updateAll(list); transaction.commit(); } catch (ActiveException e) { transaction.rollback(); e.printStackTrace(); } CREATE TABLE INVENTORY ( PARTNUMBER INTEGER PRIMARY KEY, QUANTITY INTEGER, MANUFACTURERID VARCHAR(40) ) @Wheres({ @Where(name="manf", fieldNames="manufacturerId") // where manf=? }) public class Inventory extends ActiveRecord<Inventory> { private static final long serialVersionUID = 1L; public static final ActiveTable<Inventory> table = table(Inventory.class); int partNumber; int quantity; String manufacturerId; getters/setters... }
These examples use a default data source for all active record operations. A default data source is set with
ActiveDatabase#setDefault.
You may also have multiple data sources for record objects using
ActiveRecord#attach or
implicitly when using an ActiveTable
that is constructed with a specific data source. See org.sormula.active pacakge for the active record javadoc.
See /ActiveRecord/ActiveRecordExample.java in the project or active-record-example.zip download.
Sormula can use a data source directly or look it up with JNDI. |
Reduce part 1234 inventory quantity by 1.
|
© 2018 Sormula, LLC |