Apps

Learning Oracle ADF: Master-detail forms


In my previous two posts, I covered the simplest case for Oracle ADF: building the Model and ViewController objects for a single table. Of course, that's a really basic case. What about the more common case where you've got a master-detail relationship to model? To find out how that works, I built a couple of simple tables.

First, I created a Dept table that includes a deptno (department number) primary key. Then, I have an Emp table to store employees, each of whom belongs to a department through a deptno foreign key back to the Dept table.

Next, I went through the same ADF wizards I discussed in the first article, creating separate Entity and View objects to represent each of the two tables. You end up with four objects, an Entity and a View for both the master and detail tables. My JDeveloper project ended up looking something like this:

Entity and View objects

You'll notice that there's also a couple of weirdly named objects at the end of the Model (in my case called SysC004051Assoc and SysC004052Assoc). I have no real idea what those do except that when you right-click and edit them they appear to be modeling the relationships. I have two of these because my Emp table has a foreign key on itself -- each employee has a manager and that manager is constrained to be a fellow employee. The other Association object models the deptno foreign key relationship that we're after for the purposes of this article.

Now comes the part that's really different for modeling master-detail as opposed to a single table: You have to create something called a ViewLink object. Right-click the Model folder and select New from the pop-up menu. In the Categories section, open up the Business Tier and click ADF Business Components. In the right-hand pane, you will see all of the ADF wizards -- pick the one called View Link and click OK.

View Link

On the first step through the wizard, don't just accept the default name -- make it descriptive in case you have to find it again in your project. I named mine the DeptEmpViewLink.

In the second step of the wizard, you have to describe the cardinality. In my case, it's a 1-to-many. But there's also a "0..1 to *" option in the wizard, which sounded to me like a better option. In case I had a department in my Dept table without any employees in the Emp table, that would be a 0-to-many relationship, so that's the one I picked.

In the two panes of that second step, you have to pick which fields are being related. I picked my deptno field from both tables and clicked the little Add button:

View Link Wizard

Click Next to go to the next screen of the wizard. Click the Test button to make sure everything's working. You should get a Query Is Valid dialog box. At this point, you can  click Finish to accept the rest of the defaults and create the view link object.

Now go back to the Model folder, right-click, select New, and create an Application Module (similar to what I did in the first article of this series). When you step through this wizard, you'll see that your two base objects show up, but also there's a sub-object visible attached to one of them that represents the master-detail relationship. Select the parent and move it over to the Data Model column by clicking the blue greater-than icon. Then select the child and move that over as well. It should end up looking something like this:

Create Application Module

Click Finish to accept the rest of the defaults and create your new Module. Right-click on that new module and select Test from the pop-up menu. Run through the test to make sure it's showing your master-detail correctly. You should see something like this:

Testing the AppModule

They are linked if you browse to a particular department in the master object and then click on the detail object. (It will only show you employees with the same department number.) So far, so good. Now I have to figure out how to create the UI for this.

Open the ViewController folder in your project, and then go to the Web Content folder and then the WEB-INF folder inside that. Double-click the faces-config.xml to launch the editor. Drag a JSF Page object from the Component Palette onto the faces-config.xml diagram editor. I renamed mine to /index.jsp but you don't have to do that. You can accept the default for now if you want.

Double-click the icon for your new page to launch the Create JSF JSP Wizard. As before, when you get to Step 3 in the wizard you might want to move over all the tag libraries to the right column. So they are all available to you when working with the page. At that point you can click Finish to complete the wizard.

The page editor shows up and the application module you created earlier should now be visible in the Data Control Palette on the right. Open up that application module and you'll see the master view object. Open that and it will show the individual fields of that object plus, if everything was done correctly, the detail view object will be there embeded under the master view object.

You want to drag the detail view object, not the master view object, onto your JSP page. The Create menu will pop-up, select Master Details and then pick what type of master-detail you want. I selected ADF Master Form, Detail Form and got this:

Master-detail form

When I tried running my new form this is what I got:

Running the Form

Once again there's some piece of the puzzle I'm not quite getting. Because I thought these were supposed to be text input fields, in other words I should have been able to add a Create or a Commit button and make this into a data-entry form.

But this is just a display form. I can cruise around thru the master and detail, everything shows up fine. But when I tried adding a Create action to the detail, the button was there yet when I clicked it did not convert this page into an editable form. It just re-displayed the same page.

Yet again I've come so very close, the goal is almost in reach. But I'm stymied by how to make this into an actual data-entry form. Maybe over the holiday weekend inspiration will strike and I'll figure it out.

7 comments
castrogeneris
castrogeneris

Hi, I am proposing the usage of Oracle ADF at the company I work for, We use ms sql server2008 and a lot of reporting services reports (rdls). do you know if all Jdeveloper and adf features work with the said software? thenks, be well R

vimalan28
vimalan28

Hi, I want to develop entry screen for Master-Detail table. If any sample or document is there means please share it. Thanks & Regards Vimalan Balan

rwills
rwills

I have read your previous two posts and would like to consider using this technology. However, what do you know about the integration to Oracle Apps? I understand that ADF does not support Oracle security yet and if I were to use this, I'd have to maintain two security protocols.

gevatron
gevatron

If you want to have forms that let you edit, then first drag the master view and drop it as an ADF Form and then drag the detail view and drop it either as an ADF Form or an ADF Table. The master-detail drop target creates read-only components. I would really recommend you'll go through this tutorial: http://www.oracle.com/technology/obe/obe1013jdev/10131/masterdetail_adf_bc/master-detail_pagewith_adf_bc.htm and then this one: http://www.oracle.com/technology/obe/ADFBC_tutorial_1013/10131/index.htm

Justin James
Justin James

Rex - Beware of those 0..Many relationships! Tools typically automagically generate OUTER JOINs based on that kind of relationship, which is deadly to performance. J.Ja

wizard57m-cnet
wizard57m-cnet

Try reposting this in the 'Q&A' forum. The 'Discussion' forum is for matters of general discussion, not specific problems in search of a solution. The 'Water Cooler' is for non-technical discussions. You can submit a question to 'Q&A' here: http://www.techrepublic.com/forum/questions/post?tag=mantle_skin;content There are TR members who specifically seek out problems in need of a solution. Although there is some overlap between the forums, you'll find more of those members in 'Q&A' than in 'Discussions' or 'Water Cooler'. Be sure to use the voting buttons to provide your feedback. Voting a '+' does not necessarily mean that a given response contained the complete solution to your problem, but that it served to guide you toward it. This is intended to serve as an aid to those who may in the future have a problem similar to yours. If they have a ready source of reference available, perhaps won't need to repeat questions previously asked and answered. If a post did contain the solution to your problem, you can also close the question by marking the helpful post as "The Answer".

RexWorld
RexWorld

Thank you, that makes sense now. Yeah I should go thru those tutorials but I was trying to see how much I could figure out on my own. From the tool itself. Clearly not enough :-)