Return of select objects (ComboBox) with Hibernate + JSP + VRaptor. Does not fill relationship between tables

1

I have an object TabelaPrecoItem with all the structure formed and almost everything working, follows the model model.

    @Entity
    @Table(name = "fov_tabela_preco_itens")
    public class TabelaPrecoItem implements Serializable {


    /**
     * 
     */
    private static final long serialVersionUID = 7517120126809692389L;

    @Id
    @GeneratedValue
    private int id;

    //    @Column(name = "tab_id")
    //    private Integer tab_id;

    //    @Column(name = "pro_id")
    //    private Integer id_produto;

    @Column(name = "tpi_preco")
    private Double preco;

    @Column(name = "tpi_minimo")
    private Double minimo;

    @Column(name = "tpi_maximo")
    private Double maximo;

    @NotNull(message = "Campo \"Tabela de Preço\" precisa ser informado.")
    @JoinColumn(name = "tab_id")
    @OneToOne(targetEntity = TabelaPreco.class)
    @NotFound(action = NotFoundAction.EXCEPTION)
    private TabelaPreco tabelaPreco;

    @NotNull(message = "Campo \"Produto\" precisa ser informado.")
    @JoinColumn(name = "pro_id")
    @OneToOne(targetEntity = Produto.class)
    @NotFound(action = NotFoundAction.EXCEPTION)
    private Produto produto;

Basically everything is working. The method of controler that you select is returning all objects correctly, I'm having problems is when I try to edit this object.

I created the JSP as follows

<div id="msgErroModal" class="alert alert-danger fade"
    style="display: none;" aria-hidden="true"></div>
<form id="formCadastro" method="post"  accept-charset="UTF-8" class="smart-form" onsubmit='<c:if test="${acao != \"exibir\"}">submitForm("tabelaprecoitem", "formCadastro");</c:if> return false;' action="#">
    <fieldset>
        <section>
            <div class="inline-group">
            <div class="col col-3"></div>
            </div>
        </section>
            <section>
            <div class="row">
                <label class="label col col-3 text-right"><i class="fa fa-asterisk text-danger"></i> Tabela de Preço</label>
                <div class="col col-9">
                    <label class="select">
                        <select name="tabelaPrecoItem.tabelaPreco.id" required  disabled="disabled">
                            <option value="">Selecione uma Tabela</option>
                            <c:forEach var="tabela" items="${tabelaPrecoList}">
                                <option value="${tabela.id}" <c:if test="${tabela.id eq tabelaPrecoItem.tabelaPreco.id}">selected="selected"</c:if>>${tabela.descricao}</option>
                            </c:forEach>
                        </select >
                        <i class="fa fa-group"></i>
                    </label>
                </div>
            </div>
        </section>    

        <section>
            <div class="row">
                <label class="label col col-3 text-right">ID</label>
                <div class="col col-9">
                    <label class="input"><i></i>
                        <input type="text" value="${tabelaPrecoItem.id}" disabled="disabled">
                        <input type="hidden" name="tabelaPrecoItem.id" value="${tabelaPrecoItem.id}">
                    </label>
                </div>
            </div>
        </section>        
            <section>
            <div class="row">
                <label class="label col col-3 text-right"><i class="fa fa-asterisk text-danger"></i> Produto</label>
                <div class="col col-9">
                    <label class="select">
                        <select name="tabelaPrecoItem.produto.id" required  disabled="disabled">
                            <option value="">Selecione um Produto</option>
                            <c:forEach var="produto" items="${produtoList}">
                                <option value="${produto.id}" <c:if test="${produto.id eq tabelaPrecoItem.produto.id}">selected="selected"</c:if>>${produto.descricao}</option>
                            </c:forEach>
                        </select >
                        <i class="fa fa-group"></i>
                    </label>
                </div>
            </div>
        </section>    
        <section>
            <div class="row">
                <label class="label col col-3 text-right"><i class="fa fa-asterisk text-danger"></i> Preço</label>
                <div class="col col-9">
                    <label class="input">
                        <input type="number" step="0.01" placeholder="Ex: 20" name="tabelaPrecoItem.preco" value="${tabelaPrecoItem.preco}" required>
                    </label>
                </div>
            </div>
        </section>
        <section>
            <div class="row">
                <label class="label col col-3 text-right"><i class="fa fa-asterisk text-danger"></i> Mínimo</label>
                <div class="col col-9">
                    <label class="input">
                        <input type="number" step="0.01" placeholder="Ex: 20" name="tabelaPrecoItem.minimo" value="${tabelaPrecoItem.minimo}" >
                    </label>
                </div>
            </div>
        </section>
        <section>
            <div class="row">
                <label class="label col col-3 text-right"><i class="fa fa-asterisk text-danger"></i> Máximo</label>
                <div class="col col-9">
                    <label class="input">
                        <input type="number" step="0.01" placeholder="Ex: 20" name="tabelaPrecoItem.maximo" value="${tabelaPrecoItem.maximo}">
                    </label>
                </div>
            </div>
        </section>

    </fieldset>

    <footer>
        <div class="col-md-4 text-danger pull-left">
            <i class="fa fa-asterisk"> Campo(s) Obrigatório(s).</i>
        </div>
        <c:if test="${acao != \"exibir\"}">
            <button id="salvarModal" class="btn btn-sm btn-success" type="submit"><i class="fa fa-save"></i> Salvar</button>
        </c:if>
        <button id="fecharModal" type="button" class="btn btn-sm btn-danger" data-dismiss="modal"><i class="fa fa-times"></i> Fechar</button>
    </footer>
</form>

When I fill in the objects with the

    @Get("/{id}/editarproduto")
    public void editar(Integer id) {
        TabelaPrecoItem TabelaPrecoItemAux = null;

        try {
            TabelaPrecoItemAux = this.repository.loadById(id);

        } catch (Exception e) {
            this.showErrorViewExceptionValidator(e,
                    "Falha ao carregar dados do Item da Tabela de Preços id " + id
                            + ", por favor tente novamente!", "TabelaPrecoItem");
        }

        this.result.include("acao", "editar").
        include("tabelaPrecoItem", TabelaPrecoItemAux).
        include("tabelaPrecoList",this.tabelaPrecoRepository.loadAll()).
        include("produtoList",this.produtoRepository.loadAll()).
        forwardTo(this).cadastroProduto();
    }

The screen is filled in correctly. But when I call the edit method, to write the changes to the database, the tabelaPrecoItem.produto and tabelaPrecoItem.tabelaPreco objects are returning as null .

@Post("")
    public void salvar(TabelaPrecoItem tabelaPrecoItem) {
        String message = "";
        try {
            // Realiza a validaçãoo Server-Side
            this.validator.validate(tabelaPrecoItem);

            // Verifica erros e retorna para camada de visão, caso contrario
            // segue o fluxo normal de cadastro
            if (this.validator.getErrors().size() == 0) {

                if (tabelaPrecoItem.getId() == 0) {
                    tabelaPrecoItem = repository.add(tabelaPrecoItem);
                    message = "Novo Produto da Tabela de Preços id " + tabelaPrecoItem.getId()
                            + " cadastrado com sucesso!";

                } else {
                    tabelaPrecoItem = repository.update(tabelaPrecoItem);
                    message = "Produto da Tabela de Preços id " + tabelaPrecoItem.getId()
                            + " alterado com sucesso!";
                }
            }

        } catch (Exception e) {
            this.showErrorViewExceptionValidator(
                    e,
                    "Falha ao tentar salvar dados do Produto da Tabela de Preços, por favor tente novamente!",
                    "TabelaPreco");
        }

        // retorna o objeto de retorno fora do try catch para evitar exception
        // desnecessárias
        if (!message.equals("")) {
            result.use(Results.json()).withoutRoot().from(message).serialize();

        } else {
            this.returnErrorValidatorMessage();
        }
    }

And with that it does not pass in validate .

    
asked by anonymous 16.05.2014 / 15:15

2 answers

1

At the time of loading your entity, it is not loading related entities, you can achieve this by using cascade, which is an attribute of relationship annotations, for example:

@OneToOne (cascade = CascadeType.ALL)

This causes any operation on the entity to be reproduced in related entities,

But you can limit this to a single operation with the other Cascade types: PERSISTE, MERGE, REMOVE, REFRESH

    
16.05.2014 / 16:27
0

The% wc% on the screen was% wc%, so% wc% does not identify the object. To solve I enabled comboBox before the post and I disabled again after the post.

    
08.06.2015 / 18:51