Sum of Total field of a list in Edit Screen

Hi guys.

I have the entity Venda, in which has a list of Produto_venda. This is the Edit Screen I used:

image

The intention is to get the sum of “Total” column of all the Produto_venda in the table, to the field “Total” of “Venda”. It is possible to add the products to the table, so the field “Total” for “Venda” should be refreshed.

In version 6.8.x of Cuba, it was working fine, but after version 7 doesn’t work anymore. The field “Total” is always 0. I already tried using Listeners and also Refreshing the page, which didn’t work.

Here’s the code for class Venda:

public class Venda extends BaseIntegerIdEntity {
    private static final long serialVersionUID = -3576490073138102830L;

    @Composition
    @OneToMany(mappedBy = "venda")
    protected List<Produto_venda> produto_venda;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "CLIENTE_ID")
    protected Cliente cliente;

    @Temporal(TemporalType.DATE)
    @NotNull
    @Column(name = "DATA_VENDA", nullable = false)
    protected Date data_venda;

    @Column(name = "TOTAL")
    protected Double total;

    
    public void setProduto_venda(List<Produto_venda> produto_venda) {
        this.produto_venda = produto_venda;
    }

    public List<Produto_venda> getProduto_venda() {
        return produto_venda;
    }


    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Cliente getCliente() {
        return cliente;
    }


    @Transient
    @MetaProperty(related = "produto_venda")
    protected Double total2;
    public Double getTotal2() {

    	total2 = 0.0;

        if(produto_venda == null){
            return total2;
        } else

        for (Produto_venda pv: produto_venda) {
            total2 += pv.getCusto();
        }

        setTotal(total2);
        return total2;
    }


    public void setData_venda(Date data_venda) {
        this.data_venda = data_venda;
    }

    public Date getData_venda() {
        return data_venda;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Double getTotal() {
        return total;
    }


}

The Total field I get from getTotal2 method.

Any suggestions for what to do?

Thanks in advance.

Hello guys!

Anyone has any idea?

I’m trying to do something on VendaEdit, but nothing works.

If someone knows what to do, would help.

Thanks!

Hi,
I would suggest using the approach explained in Quick Start, see section 3.4 ADDING EDITING FUNCTIONALITY TO THE UI here.
I.e. use listeners in the UI screen instead of the entity attribute, because its getter is not invoked when the list is changed.