Gives success message that saved, not error message, but in the database is not saved - jsf

Asked

Viewed 563 times

0

My code is giving message of success in saving the record, but in reality it is not saving, I have already checked in the database, is not generating error message, and presents the code all right, I wonder what is happening for this to happen?

My class Bean

package br.com.terezinha.imobiliaria.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.terezinha.imobiliaria.model.Cidade;
import br.com.terezinha.imobiliaria.repository.Cidades;
import br.com.terezinha.imobiliaria.services.CadastroCidadeService;
import br.com.terezinha.imobiliaria.util.FacesUtil;

@Named
@ViewScoped
public class CadastroCidadeBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Cidade cidade;


    @Inject
    private CadastroCidadeService cadastroCidadeService;


    @Inject
    private Cidades cidades;

    public CadastroCidadeBean() {
        cidade = new Cidade();
    }

//    public void inicializar() {
//        System.out.println("iniciaando ........");
//        if (FacesUtil.isNotPostback()) {
//            todasCidades = cidades.raizesC();
//        }
//    }
//    

    public void salvar(){
        this.cidade = cadastroCidadeService.salvar(cidade);
        FacesUtil.addInfoMessage("Cidade salvo com sucesso");
    }

    public Cidade getCidade() {
        return cidade;
    }

    public void setCidade(Cidade cidade) {
        this.cidade = cidade;
    }


    public Cidades getCidades() {
        return cidades;
    }

    public void setCidades(Cidades cidades) {
        this.cidades = cidades;
    }

}

My class who entered the register;

package br.com.terezinha.imobiliaria.repository;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import br.com.terezinha.imobiliaria.model.Cidade;

public class Cidades implements Serializable{

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

    @Inject
    private EntityManager manager;

    public Cidade porId(Long id) {
        return manager.find(Cidade.class, id);

    }


    public List<Cidade> raizesC(){
        return manager.createQuery("from Cidade order by nome", Cidade.class).getResultList();
    }



    public Cidade guardar(Cidade cidade) {
        return manager.merge(cidade);
    }



}

///////////////////////////////

*/
private static final long serialVersionUID = 1L;


@Inject
private Cidades cidades;


@Transactional
public  Cidade salvar(Cidade cidade){
    return cidades.guardar(cidade);
}

My page

<ui:composition template="/WEB-INF/template/LayoutPadrao.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <ui:define name="titulo">Novo produto</ui:define>

    <ui:define name="corpo">



        <h:form>

            <h1>Nova Cidade</h1>

            <p:messages autoUpdate="true" closable="true" />

            <p:toolbar style="margin-top: 20px">
                <p:toolbarGroup>
                    <p:button value="Novo" />
                    <p:commandButton value="Salvar" id="botaoSalvar"  action="#{cadastroCidadeBean.salvar}" update="@form"/>
                </p:toolbarGroup>
                <p:toolbarGroup align="right">
                    <p:button value="Pesquisa" />
                </p:toolbarGroup>
            </p:toolbar>

            <p:panelGrid columns="2" id="painel"
                style="width: 100%; margin-top: 20px" columnClasses="rotulo, campo">
                <p:outputLabel value="Nome da cidade" for="cidade" />
                <p:inputText id="cidade" size="20" maxlength="20"
                    value="#{cadastroCidadeBean.cidade.nome}" />


            </p:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>

The path of my project in the Github repository

https://github.com/wladimirbandeira/Terezinha.Adm

I made these attempts

attempt a;

public void salvar(){
        this.cidade = cadastroCidadeService.salvar(this.cidade);
        System.out.println("valor da cidade no pacote controller  " + cidade.getNome());
        FacesUtil.addInfoMessage("Cidade salvo com sucesso");
    }

attempt two;

public Cidade guardar(Cidade cidade) {
     cidade =  manager.merge(cidade);

     System.out.println("valor da cidade no pacote repositório " + cidade.getNome());
    return cidade;
}

attempt three;

@Transactional
public Cidade salvar(Cidade cidade) {
    cidade = cidades.guardar(cidade);
    System.out.println("valor da cidade no pacote de negotios  " + cidade.getNome());
    return cidade;

}

resultant;

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
valor da cidade no pacote repositório Caruaru
valor da cidade no pacote de negotios  Caruaru
valor da cidade no pacote controller  Caruaru
  • It’s a new town with id == null right? Try with persist instead of merge for diagnostic purposes and post to us the SQL commands generated by Hibernate.

  • Another thing, your code is using CDI Producers, Interceptors and his own note @Transactional to do something that a container EJB or a framework as Spring gives you for free. Although you understand the desire to adopt a strategy lightweight, it will be worth trying to reinvent this type of mechanism?

  • my id is not equal to null. It was completely unfounded to have to replace Meirger with persist because in fact you didn’t even get to check my code right on Github. and another ,I see nothing wrong with using CDI Producers, Interceptors in @Transactional annotation, you treat my situation as Mples. It’s not easy for me to do in EJB and Spring, I wish you could help me inside my reality.

  • I don’t usually respond to that kind of comment, but I would say that if you expect someone to do more than take a diagonal look at your final Github code your expectation is wrong. You are responsible for creating a mvce, I’m just trying to teach you how to diagnose the problem, as well as measure the pros and cons of your architectural decisions. Again, take a slow look at your CadastroClienteBean, is sure that the Cidade has codigo (the id) other than null when you call cadastroCidadeBean.salvar?

  • All right, thank you very much, I’ll check on that.

2 answers

1


From what I can tell from the code, your programming logic is displaying the "saved" message without even testing whether this object was actually saved. Probably you are forgetting to handle the Transactions with the Database, as I have not seen in your code any Initialization and Commit treatment of transactions with the Database. Try starting a transaction and then committing in the method where the city is saved.

0

In the city class guard method is missing the

transaction.begin();
if(merge()){
  transaction.commit();
}else{
  transaction.rooback();
}
  • 2

    Hello Luiz, welcome to [en.so]! Pode [Edit] a resposta para formatar melhor o código. If it is possible to explain a little more the answer would be excellent.

Browser other questions tagged

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