Friday, May 6, 2011

Master – Detail saving records - Integrity constraint violated and parent key not found


This is the use case in which developers will get error while committing the transaction. The use case is , there is a page having master viewobject as form and Details viewobject as table. Creating the new record in the master form and detail, while saving the error arises like “integrity constraint (X.XYZ_FK) violated - parent key not found” here the detail table expects the foreign key value for the new record, the transaction commits before master details posted into the database table. You can solve this issue using Create for master form and CreateInsert for detail. Download source
  1. Create the two Entity based View Objects having master detail relationship, for example our usual HR schema
Employees and Departments(having Department_Id as foreign key relationship)

2. Drag and Drop those view objects into the pages, Department as Form and Employees as Table.

3. Drag and drop the Create operation for master and CreateInsert for the Detail as adf button in the page.

4. At last from the common operation drag and drop the Commit as adf button in the page.

5. Run the application and create the new record for the master




6.And create the new record for the Detail in the Grid, and enter the same key value in the master DeparmentId and commit the record





7. In the database you can find the new inserted record

Create and CreateInsert :
The difference between Create and CreateInsert operation is create operation

Create(CretateRow)
This operation is same as Create but, it create and inserts the new record in the collection partially(inserts the row in the component fields).

CreateInsert (CreateInsertRow)
The new record is created and added into the collection when the page is submitted.
The new row is get set in the iterator and it inserts before the current row.


You can also change the order of the transaction using postchanges method in the impl class

Overriding postchanges(Transactionevent e) in the Impl class:

public void postChanges(TransactionEvent e) {
/* If current entity is new or modified */
if (getPostState() == STATUS_NEW ||
   getPostState() == STATUS_MODIFIED) {
 /* Get the associated product for the service request */
 DepartmentsImpl dept = getDepartments();

 /* If there is an associated product */
 if (dept != null) {
   /* And if it's post-status is NEW */
   if (dept.getPostState() == STATUS_NEW) {
     /*
      * Post the product first, before posting this
      * entity by calling super below
      */
     dept.postChanges(e);
   }
 }
}
super.postChanges(e);
}


For Use cases like this, we can use these operations well. You can also refer more about Create and Create insert in this blog http://jjzheng.blogspot.com/2010/04/create-or-createinsert.html

Also refer this for more about master-detail relationship use cases

http://andrejusb.blogspot.com/2010/04/adf-magic-or-bad-database-design-for.html


http://andrejusb.blogspot.com/2009/03/create-operation-for-master-detail.html

1 comment:

  1. Your website is very good and nice information was provided in your site, thanks for sharing.Oracle ADF Training in Hyderabad

    ReplyDelete