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:
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.
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:
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:
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:
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:
When I tried running my new form this is what I got:
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.