2010年11月23日星期二

Update a delete flag instead of directly delete data from database

Technorati Tags: ,

In some scenario, it’s important and necessary to update a delete flag in the database table instead of directly delete the row data from the table.

First let’s see there is a QUESTIONS table with a DELETED column in the database. The DELETED column is a CHAR(1) with a default value of ‘N’.

Step 1. generate the Java code of the EO

Step 2. override the remove() method in the EO

@Override
public void remove() {
    this.setDeleted("Y");
    super.remove();
}

Step 3. override the doDML() method in the EO

@Override
protected void doDML(int operation, TransactionEvent transactionEvent) {
    if(operation == DML_DELETE){
      operation = DML_UPDATE;
    }
    super.doDML(operation, transactionEvent);
}

Step 4. change the query SQL of the VO, add the DELETE = ‘N’ condition.

Now, when delete an entity in the BC browser, the row data is still in the database, but with DELETED column’s value equals ‘Y’.

First, the remove() method is executed when try to remove a record. Then when commit the transaction, the doDML() method is executed for every row.