Insert, Update and Delete Operations on the Database

A DO that is newly created (insert operation) with a transaction parameter, holds its transaction information.

A DO that is newly created (insert operation) without a transaction parameter is created in an implicit DB transaction, but DO holds no transaction in the memory.

If DOs are deleted with DO.delete(), they are marked as deleted (DO attribute deleted set to true). These DOs can not be returned any more from the local transaction DO cache, and methods DO.getXXX() throw an exception (because the DO is already deleted in the transaction).

Added new methods: unDelete() and unDelete(DBTransaction dbt). These methods undelete deleted DO by setting DO's attribute deleted to false and inserting the DO to the database.

If the unDelete method is called after cascade delete, only the root DO is undeleted.

Added new method: undo(). This method returns DO in the state in which it had been before the last commit, if there was any commit performed. If there was not any commit performed, DO is returned in the "empty" state (it has no data loaded).

The use of undo() method has only sense if the DO belongs to a transaction. It this method is called for a DO without a transaction, the method throws DataObjectException with a message indicating that an error has occured during the undo operation.

Repeated calls of undo() method must be separated with at least one call of commit method.

Old DODS code always updated rows, making SQL Update statement that contained all columns of a DO. This kind of operation tends to be a bit inefficient, when only a few of a dozen of columns have been changed.

Worst case scenario is: only one column changed, yet all columns get updated.

New feature in DODS is that a DO makes update statement that only includes columns that have been changed. For every column there is new boolean data member of <table_name>DO class: changedColumnName. Its value represents current state of that column. If it is true, column needs update, otherwise it doesn't.

Methods for writing to the database (insert, update and delete) reset these values to false, while methods for changing column value set appropriate changedColumnName to true.

All updates of the same DO (that are done one after another) are aggregated into one update. When insert, update or delete of another DO occurs, the aggregation is finished. If AutoWrite is true, the agregated update is performed, the DO version is increased. For AutoWrite set to false, DO remains waiting (in transaction) for explicit write() or commit().

Sample code:

DBTransaction dbTrans = DODS.getDatabaseManager().createTransaction();
DiscDO disc1 = DiscDO.createVirgin(dbTrans); 
disc1.setArtist("Artist1"); 
disc1.setTitle("Disc test1"); 
disc1.setGenre("pop1"); 
disc1.setOwner(person1); 
disc1.setIsLiked(false); 
disc1.save(); person1.delete(); 
/*undelete PersonDO */ 
person1.unDelete();