Wednesday, August 27, 2008

Soft delete with ADF entities

Say that you have a table created with this script,

create table users(user_name varchar2(20) primary key, name varchar2(100),active_yn varchar2(1) default 'Y')

And you want to set the active_yn to N when a delete occurs instead of doing a hard delete.

I found this solution working, I am still not sure whether it is the best way or not..

I created my entity by using the wizard . Then edit the entity and on the Java tab select the below check boxes to generate the required Java classes.

1-Generate entity object class
2-Remove method
3-Data manipulation methods

Then in the generated Java file change the remove and doDml methods as below

/**Add entity remove logic in this method.
*/
public void remove() {
setActiveYn("N");
super.remove();
}

/**Custom DML update/insert/delete logic here.
*/
protected void doDML(int operation, TransactionEvent e) {
if (operation != DML_DELETE)
super.doDML(operation, e);
else
super.doDML(DML_UPDATE, e);
}

-----

Then test your application and see that instead of doing a hard delete we are now setting a flag on the record when it is deleted.

No comments: