Error trying to save data to database via Java

Asked

Viewed 47 times

1

I am trying to save data in the database and is returning the following error:

Attempt to create merge Event with null Entity

The Bean is like this:

public void SalvarCliente(){
try {

ClienteDao clienteDao = new ClienteDao(); 

clienteDao.salvarCliente(cliente);
cliente = new Cliente();
System.out.println("Cliente salvo com sucesso.");
Messages.addGlobalInfo("cliente salvo com sucesso!");

} catch (RuntimeException erro) {
// mensagem de erro para o usuario 
Messages.addGlobalError("ocorreu um erro ao tentar salvar o cliente");
erro.printStackTrace();  
}
}

The XHTML thus:

<h:form id="formCadastro">
<p:panel header="Cadastro" >
<p:panelGrid columns="2" >


<p:outputLabel value="Nome:" />
<p:inputText value="#{clienteBean.cliente.nome}" />

<p:outputLabel value="Telefone:" />
<p:inputText value="#{clienteBean.cliente.telefone}" />

<p:outputLabel value="CPF:" />
<p:inputText value="#{clienteBean.cliente.cpf}" />

<p:outputLabel value="Cidade:" />
<p:inputText value="#{clienteBean.cliente.cidade}" />
<p:commandButton value="Salvar" action="#{clienteBean.SalvarCliente}" process="@this" />

</p:panelGrid>

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

customer class:

@Entity
public class Cliente {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
    private String nome;
    private String telefone;
    private String cidade;
    private String cpf;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public String getCidade() {
        return cidade;
    }

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

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Cliente other = (Cliente) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

}
  • o xhtml assim :&#xA;<h:form id="formCadastro">&#xA;<p:panel header="Cadastro" >&#xA;<p:panelGrid columns="2" >&#xA;&#xA;<p:outputLabel value="Nome:" />&#xA;<p:inputText value="#{clienteBean.cliente.nome}" />&#xA;&#xA;<p:outputLabel value="Telefone:" />&#xA;<p:inputText value="#{clienteBean.cliente.telefone}" />&#xA;<p:outputLabel value="CPF:" />&#xA;<p:inputText value="#{clienteBean.cliente.cpf}" />&#xA;&#xA;<p:outputLabel value="Cidade:" />&#xA;<p:inputText value="#{clienteBean.cliente.cidade}" />&#xA;&#xA;&#xA;&#xA;<p:commandButton value="Salvar" action="#{clienteBean.Salvarclient}" process="@this" /> </p:panelGrid> </p:panel> </h:form>

  • How is the class Cliente? Are you using some ORM as Hibernate?

  • am yes, Entity public class Client { Id Generatedvalue(Strategy=Generationtype.IDENTITY) private Long id;etc...

  • Put this class in the text of the question

  • put , I believe the problem is my xhtml, I made another with layout with oncomplete="PF('dialogo'). show();" and widgetVar="dialogo" and worked normally

1 answer

0

Check that your Client class is annotated with @Entity. Whether it has a variable annotated with @Id and @Generatedvalue

As in the example of documentation: @Generatedvalue

  • this yes. has the Entity , id and generateValue

Browser other questions tagged

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