Set an attribute of another class

Asked

Viewed 160 times

0

I am building a list in JSF, which uses attributes of two classes (ItemdoPedido and Produto).

Man ManagedBean is class-based ItemdoPedido, then the difficulty is in setting and recovering the class value Produto.

Classes of the model:

public class Produto{

    public int id;
    public String descricao;
    /* getters and setters */
}

public class ItemDoPedido{

    private int id;
    private int quantidade;
    private Produto p;
}

This is my DAO who builds the list and is called in the bean:

public class ItemdoPedidoDAO {    
      public List<ItemDoPedido> MostrarPedidos() {

            Connection con = Conexao.getConnection();

            PreparedStatement stmt = null;
            ResultSet rs = null;

            List<ItemDoPedido> itens = new ArrayList<>();

            try {
                stmt = con.prepareStatement("select p.id as id_pedido, i.qtdade as qtdade, r.descricao as descricao\n"
                        + "from cliente c inner join pedido p on c.id = p.id_cliente\n"
                        + " join item_do_pedido i on p.id = i.id_pedido\n"
                        + " join produto r on i.id_produto = r.id\n"
                        + " where c.cpf = '982139812'"
                        + "");
                rs = stmt.executeQuery();

                while (rs.next()) {

                    ItemDoPedido i = new ItemDoPedido();
                    Produto produto = new Produto();

                    i.setId(rs.getInt("id_pedido"));
                    i.setQuantidade(rs.getInt("qtdade"));
                    i.setP(produto.setDescricao(rs.getString("descricao")));
                    itens.add(i);
               }        
            } catch (SQLException ex) {
                Logger.getLogger(ClienteDAO.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                Conexao.closeConnection(con, stmt, rs);
            }
            return itens;
        }
    }

The problem is in the class above, in the line:

i.setP(produto.setDescricao(rs.getString("descricao")));

She makes the following mistake:

VOID TYPE NOT ALLOWED HERE

This is my bean, getters and Setter do not appear but are included:

@ManagedBean(name="pedidoMB")
@SessionScoped
public class pedidoMB {

    private ItemDoPedido item;
    private Produto produto;
    private List<ItemDoPedido> itens;
    private ItemdoPedidoDAO iDAO = new ItemdoPedidoDAO();


    public pedidoMB() {
        item = new ItemDoPedido();
        produto = new Produto();
        itens = iDAO.MostrarPedidos(); 
    }

This is the part of my screen that shows the data:

<h:form> 
            <p:panel header="Pedidos">
                <p:link outcome="index" value="Voltar"></p:link>
                <p:dataTable var="item" value="#{pedidoMB.itens}">
                <p:column  headerText="Id">
                    <h:outputText value="#{item.id}" />
                </p:column>

                 <p:column  headerText="Quantidade">
                     <h:outputText value="#{item.quantidade}" />
                </p:column>

                <p:column  headerText="Produto">
                    <h:outputText value="#{item.p.descricao}" />
                </p:column>

            </p:dataTable>
            </p:panel>
        </h:form>

In the end, my idea is to exfil something like this:

ID | QUANTIDADE | PRODUTO
1  |    10      | CADERNO
2  |    20      | CANETA 

1 answer

0


If the getters and setters were implemented in the "traditional" way, I imagine they are like this:

public class Produto{
    ... // demais campos, setters, getters, etc
    public String descricao;

    public void setDescricao(String desc) {
        this.descricao = desc;
    }
}

public class ItemDoPedido{
    ... // demais campos, setters, getters, etc
    private Produto p;

    public void setP(Produto p) {
        this.p = p;
    }
}

Without getting into the merits of having a method called setP, which is a pretty bad name (wouldn’t it be better to be setProduto?), but finally...

Notice that setDescricao is a method void, ie, it does not return any value. And when doing:

i.setP(produto.setDescricao(rs.getString("descricao")));

You’re calling setP (waiting for a Produto as parameter), but instead is passing the return of the method setDescricao. Only that setDescricao does not return anything, because it is a method void, hence the error of "void type not allowed here".

To work, just separate the calls and pass the correct parameter to setP:

produto.setDescricao(rs.getString("descricao"));
i.setP(produto);
  • 1

    Thanks a lot man, with your explanation I really understood, tested and worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.