Skip navigation links

Package org.sormula.active

An implementation of the active record pattern built on top of sormula.

See: Description

Package org.sormula.active Description

An implementation of the active record pattern built on top of sormula. Classes in this package may be used as an independent alternative to other classes in sormula.

ActiveRecord objects know about their ActiveDatabase so they may be inserted, updated, deleted by simply calling the inherited methods ActiveRecord.save(), ActiveRecord.insert(), ActiveRecord.update(), and ActiveRecord.delete(). An active record may also be processed by analogous methods in ActiveTable, ActiveTable.save(ActiveRecord), ActiveTable.insert(ActiveRecord), ActiveTable.update(ActiveRecord), ActiveTable.delete(ActiveRecord).

ActiveTable can find active records and operate upon a Collection of active records.

For example:

   // get part by primary key
   Inventory inventory = Inventory.table.select(partNumber);
         
   // update
   inventory.setQuantity(inventory.getQuantity() - 42);
   inventory.update();
   
For all active record and active table methods that modify the database, if no ActiveTransaction is used and the connection obtained from DataSource has autocommit turned off, then an ActiveTransaction will be created for the duration of each active record method. Therefore you can use the active record methods with or without an ActiveTransaction. However it is probably better to group related active record methods into a single ActiveTransaction.

For example:

    ActiveTransaction transaction = new ActiveTransaction(new ActiveDatabase(dataSource));
    
    try
    {
        transaction.begin();

        // get part by primary key
        Inventory inventory = Inventory.table.select(partNumber);
             
        // update
        inventory.setQuantity(inventory.getQuantity() - 42);
        inventory.update();
        
        transaction.commit();
    }
    catch (ActiveException e)
    {
        transaction.rollback();
    }
 
Skip navigation links