Its very frequent requirement in Oracle ADF applications, when developer needs to implement Master-Detail relationship with Create functionality. Actually, its very easy to implement Master-Detail in ADF, the same is for Create operation. However, there is one thing you should know, especially if you are new to ADF. Let's say you are implementing functionality to allow users to insert data into Master and into Detail, its fine - you can just drag and drop Create or CreateInsert action from Data Control. But you will face a problem, when user will try to insert Master and Detail record during the same transaction:
There will be a problem, because framework will try to commit Detail record without any value for foreign key. There will be no value for foreign key, because Master record is still not committed and sequence number is not assigned for Master record primary key. When user will try to close transaction by committing new records from Master and Detail, integrity constraint violation error will be thrown by database:
From my practice, its quite common problem and many developers are facing this. However, there is one feature in Oracle ADF, that helps to solve described problem. You just need to open Entity Association wizard for those Entities involved into Master-Detail relationship:
And basically to select one checkbox in Behavior group, this checkbox is Composition Association. By selecting this option, you will force framework to use so called strong association between two Entities. With strong association, framework automatically will ensure correct foreign key value propagation for Detail record:
And there is no additional changes, Create functionality now will work for both - Master and Detail in the same transaction. Just make sure you didn't forgot to set DBSequence type for Primary Keys in your Entities. Both new records, Master and Detail, now can be commited together:
It works, but its important to know - when you are using Composite Association, you can't use Detail Entity without Master Entity in the same page. You can try to put Detail (in my case Departments Entity) into separate page and to invoke any CRUD operation, you will get Invalid Owner Exception:
More about this case you can read from Steve Muench blog - Why Do I Get the InvalidOwnerException?.
You can download and run my sample application - CreateMasterDetail.zip, developed with JDeveloper 11g for this post.
El desarrollo de pantallas master-detail son comunes y sencillas de realizar usando ADF. Sin embargo, si se pretende grabar simultaneamente ambos registros, saldrá un error debido a que requerirá grabar primer la información que esta en cabecerá y luego la información que se encuentra en el detalle. Para evitar la salida de un error indecifrable, Andrejus nos muestra un tip muy útil en este post.