Master / Detail Pages with Netbeans 6.1 and Java Server Faces

A lot of times you will want to create a page that has a header and a table that is a child, such as a purchase order where you have a “Purchase order header” and “Purchase Order Lines”

In this case I have a master detail form here as you can see. Now. To make sure the detail form only has the data in from THAT purchase order I insert some code into the init() function of the page.

 try {
            String sSQL="SELECT ALL polines.id, polines.poid, polines.description, polines.qty, polines.price, polines.total  FROM polines WHERE poid=" + cachedRowSetDataProvider1.getValue("id");
            polinesDataProvider.getCachedRowSet().setCommand(sSQL);
            polinesDataProvider.getCachedRowSet().execute();
            polinesDataProvider.refresh();
              } catch (Exception e) {
                  
              }

(polinesDataProvider is the rows of the table, the “detail part” if you like, cachedDataProvider1 is the header)

Now here’s the tricky part. Normally I would do an ADDNEW and just do:-

 RowKey newRow = polinesDataProvider.appendRow();
        polinesDataProvider.setCursorRow(newRow);
        polinesDataProvider.setValue("poid", cachedRowSetDataProvider1.getValue("id"));
        return null;

But I discovered that when you do the “CommitChanges()” command the new row is not saved because there is a WHERE in the command query I set earlier. This is a bug (http://www.netbeans.org/issues/show_bug.cgi?id=139024), in fact the software just ignores the command (I got my MYSQL server to log the queiries and nothing happens).

So the workaround code for the New Item (new detail line) button is:-

        RowKey newRow = polinesDataProvider.appendRow();
        polinesDataProvider.setCursorRow(newRow);
        polinesDataProvider.setValue("poid", cachedRowSetDataProvider1.getValue("id"));
        polinesDataProvider.commitChanges();
        polinesDataProvider.refresh();
        return null;

Note that I set the poid field to the header’s PrimaryKey otherwise it will not look up correctly.

Strangely If you edit a record it works absolutely fine with the default just “commitingchanges” when someone clicks save.

Advertisements

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on July 4, 2008, in Howto and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: