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.
