users@glassfish.java.net

Re: VWJSF-CRUD, deleting rows from two related tables

From: <glassfish_at_javadesktop.org>
Date: Sat, 02 May 2009 00:27:29 PDT

Hi,

I was thinking and googling and I have found the solution.

This is the code,

    public String borrar_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        form1.discardSubmittedValues("salvar");
        try {
            int categoriaId = ((Integer)categoriaDD.getSelected());
            RowKey rk = tableRowGroup1.getRowKey();
            int subcategoriaId = (Integer)subcategoriaDataProvider.getValue("SUBCATEGORIA.ID_SUBCATEGORIA", rk);
            String[] fieldIds = {"CATEGORIA_SUBCATEGORIA.ID_SUBCATEGORIA", "CATEGORIA_SUBCATEGORIA.ID_CATEGORIA"};
            Object[] values = {new Integer(subcategoriaId), new Integer(categoriaId)};

            rk = categoria_subcategoriaDataProvider.findFirst(fieldIds, values);
            if (rk != null){
                categoria_subcategoriaDataProvider.removeRow(rk);
                categoria_subcategoriaDataProvider.commitChanges();
                categoria_subcategoriaDataProvider.refresh();
            }

            rk = tableRowGroup1.getRowKey();
            if (rk != null) {
                subcategoriaDataProvider.removeRow(rk);
                subcategoriaDataProvider.commitChanges();
                subcategoriaDataProvider.refresh();
            }
        } catch (Exception ex) {
            log("ErrorDescription", ex);
            error("Error: " + ex.getMessage());
        }

        return null;
    }

What I do is to get the Id (PK) from the DropDownList that take me to the Table. Into the table I click the button in the row a want to delete. That is the reason,

RowKey rk = tableRowGroup1.getRowKey();
            int subcategoriaId = (Integer)subcategoriaDataProvider.getValue("SUBCATEGORIA.ID_SUBCATEGORIA", rk);

gives me the the Id (PK) I was looking for.

Later I look for the row in the relation table CATEGORIA_SUBCATEGORIA,

String[] fieldIds = {"CATEGORIA_SUBCATEGORIA.ID_SUBCATEGORIA", "CATEGORIA_SUBCATEGORIA.ID_CATEGORIA"};
Object[] values = {new Integer(subcategoriaId), new Integer(categoriaId)};
rk = categoria_subcategoriaDataProvider.findFirst(fieldIds, values);

Once I have the RowKey, I remove first the row in the relation table and secondly I remove the row from the principal table.

Hoping it helps,

Best Regards,
Jose Alvarez de Lara
[Message sent by forum member 'josealvarezdelara' (josealvarezdelara)]

http://forums.java.net/jive/thread.jspa?messageID=344726