Tuesday, May 17, 2011

Composite Associations

When you create entity object based on database table having FK (Foreign Key Constraints) then by default following Association object behavior will be created between two table.

CREATE TABLE table_name
(
   column_id number(10),   
   column1 varchar2(20),
   FOREIGN KEY (column_id)
   REFERENCES table_23(xxxx_id)   
);

No Composite Association

image

If we try to delete a record , let us say we are deleting department that is having associated employees then it will throw or we are deleting order which has orderitems then following exception will be thrown because we cannot delete the parent until child record are present.

oracle.jbo.DMLConstrainstException

image

Implement Cascade Delete

If we select this option and tries to delete department that has associated employee or let us say try to delete order which has multiple orderitems then orders and orderitems get deleted without throwing DMLConstrainstException.

Because here this check delete all associated child record and then parent record.

image

This setting will fire DELETE statement in transaction commit time to make permanent changes.

Optimize for Database Cascade Delete

CREATE TABLE table_name
(
   column_id number(10),   
   column1 varchar2(20),
   FOREIGN KEY (column_id)
   REFERENCES supplier(column_id)
   ON DELETE CASCADE
);

When we generate BC and if our constraint in database has ON DELETE CASCADE then by default Composite Association is created with following check.

As we can see ‘Optimize for Database Cascade Delete’ is checked and Implement Cascade Delete is checked ,but disabled.

image

DELETE statement will not be fired during transaction on assumption that database has ON DELETE CASCADE constraint that will handle the deletion of corresponding rows.

No comments:

Post a Comment