Saturday, February 4, 2012

Many-to-Many Relationship Implementation in ADF BC and ADF Faces UI

Is not always obvious how to implement Many-to-Many use case. I will describe how to represent Many-to-Many data structure in ADF BC and how to display it effectively with ADF Faces. With my next post I will provide another sample application to describe how to create and delete rows for Many-to-Many.

Sample application - ManyToManyADFBC.zip is based on custom DB schema, SQL scripts are included. Database schema is taken from old 2007 post - Many-to-Many mapping in Oracle TopLink:


When you generate ADF BC, it doesn't generate Many-to-Many associations for you by default - you will need to recreate them manually:


Define * to * cardinality for Blogs -> Readers relationship, select intersection key from central mapping table:


Do the same * to * cardinality for Readers -> Blogs relationship, select intersection key from central mapping table:


Define View Link for Blogs -> Readers based on existing Association:


Create View Link for Readers -> Blogs based on existing Association:


Application Module should contain Data Model for Blogs -> Readers and Readers -> Blogs:


While Application Module contains 4 View Object instances - ADF UI implements only 2 tables. One table displays Readers data, another Blogs:


Both tables are pointing to detail View Object instances. Detail data is refreshed from master View Object instance. Current row for master View Object instance is set through choice list selection. Once choice list selection is updated and master current row is changed - ADF UI automatically updates corresponding detail View Object collection:


As you will find in Page Definition, Readers table is populated from detail View Object instance:


Master choice list is mapped with master View Object instance - it controls Readers table:


One more cross selection event is implemented by overriding table selection listeners for both Readers and Blogs tables:


Listener gets currently selected row from the table and passing row key to update current row for master collection on other association end (it will update choice list).

This is how it looks on ADF UI - two tables from Many-to-Many relationship, together with master data choice lists:


User can change Reader choice list selection, Blogs table data will be updated:


When user is changing row selection in the table itself, for example Blogs - this triggers refresh for Blog choice list on the left and Readers table as well:


Same logic with Readers table - row selection triggers refresh for Reader choice list on the right and corresponding Blogs table:


4 comments:

subbu said...

Hi Andrejus thanks for the blog..
can you give some inputs for the following scenario.

For example In creating the blog form i can give ownership to multiple members.
In the form an ComboBoxwith list of values of "list of Persons" should display for the filed 'Owner'.From there users can search and select multiple persons for the ownership.

How can i implement this like how to capture multiple persons and send to Model for storing them.
Thanks In Advance
Subbu

Tor said...

Thankyou! Exactly what I've been looking for.

QueryLarsBauer said...

Hi,
thanks for your blog.
Do you have any idea how to implement these bindings when you have additional attributes in the intersection table? I am really struggling with this.
Thank in advance!

Unknown said...

How can we add this behaviour when using the views that are involved in the N - N relationship as LOV in another view?