I am having problems, in the project I am developing in jsf using jpa and hibernate. I am not able to register the customer data in my database how can I relatify the registration?
Code
EntityManagerFactory factory = Persistence.createEntityManagerFactory("WebApplication3PU");
EntityManager manager = factory.createEntityManager();
ClienteWeb cli= new ClienteWeb();
public ClienteWeb cadastrarCliente(){
manager.getTransaction().begin();
manager.persist(cli);
manager.getTransaction().commit();
manager.close();
return cli;
}
Customer Registration
public ClienteWeb cadastrarCliente(){
manager.getTransaction().begin();
manager.persist(cli);
manager.getTransaction().commit();
manager.close();
return cli;
}
Method with objects returned from the jsf form.
public Cliente montaCliente(){
Cliente cli= new Cliente();
cli.setNome(cli.getNome());
cli.setEmail(cli.getEmail());
cli.setSexo(cli.getSexo());
cli.setEstado(cli.getEstado());
cli.setSenha(cli.getSenha());
cli.setData(cli.getData());
cli.setCelular(cli.getCelular());
cli.setCpf(cli.getCpf());
cli.setCidade(cli.getCidade());
cli.setCep(cli.getCep());
cli.setConfirmar(cli.getConfirmar());
cli.setRua(cli.getRua());
cli.setNumero(cli.getNumero());
cli.setBairro(cli.getBairro());
cli.setUf(cli.getUf());
cli.setLogin(cli.getLogin());
return cli;
}
My question is how do I put the values of the objects entered by the user that I get with my class of the magnedBean client and put in the method register Client, where in it I persist the database of my class of entity Web Client? >
My entity class
package Entity;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "ClienteWeb")
@XmlRootElement
@Entity
@Table(name = "ClienteWeb")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "ClienteWeb.findAll", query = "SELECT c FROM
ClienteWeb
c"),
@NamedQuery(name = "ClienteWeb.findByCodigo", query = "SELECT c FROM
ClienteWeb c WHERE c.codigo = :codigo"),
@NamedQuery(name = "ClienteWeb.findByEmail", query = "SELECT c FROM
ClienteWeb c WHERE c.email = :email"),
@NamedQuery(name = "ClienteWeb.findByNome", query = "SELECT c FROM
ClienteWeb c WHERE c.nome = :nome"),
@NamedQuery(name = "ClienteWeb.findBySexo", query = "SELECT c FROM C
ClienteWeb c WHERE c.sexo = :sexo"),
@NamedQuery(name = "ClienteWeb.findByEstado", query = "SELECT c FROM
ClienteWeb c WHERE c.estado = :estado"),
@NamedQuery(name = "ClienteWeb.findBySenha", query = "SELECT c FROM
ClienteWeb c WHERE c.senha = :senha"),
@NamedQuery(name = "ClienteWeb.findByData", query = "SELECT c FROM
ClienteWeb c WHERE c.data = :data"),
@NamedQuery(name = "ClienteWeb.findByCelular", query = "SELECT c FROM
ClienteWeb c WHERE c.celular = :celular"),
@NamedQuery(name = "ClienteWeb.findByCpf", query = "SELECT c FROM
ClienteWeb
c WHERE c.cpf = :cpf"),
@NamedQuery(name = "ClienteWeb.findByCidade", query = "SELECT c FROM
ClienteWeb c WHERE c.cidade = :cidade"),
@NamedQuery(name = "ClienteWeb.findByCep", query = "SELECT c FROM
ClienteWeb
c WHERE c.cep = :cep"),
@NamedQuery(name = "ClienteWeb.findByConfirmar", query = "SELECT c FROM
ClienteWeb c WHERE c.confirmar = :confirmar"),
@NamedQuery(name = "ClienteWeb.findByRua", query = "SELECT c FROM
ClienteWeb c WHERE c.rua = :rua"),
@NamedQuery(name = "ClienteWeb.findByNumero", query = "SELECT c FROM
ClienteWeb c WHERE c.numero = :numero"),
@NamedQuery(name = "ClienteWeb.findByBairro", query = "SELECT c FROM
ClienteWeb c WHERE c.bairro = :bairro"),
@NamedQuery(name = "ClienteWeb.findByUf", query = "SELECT c FROM ClienteWeb
c WHERE c.uf = :uf"),
@NamedQuery(name = "ClienteWeb.findByLogin", query = "SELECT c FROM
ClienteWeb c WHERE c.login = :login")})
public class ClienteWeb implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "codigo")
private Integer codigo;
@Column(name = "email")
private String email;
@Column(name = "nome")
private String nome;
@Column(name = "sexo")
private String sexo;
@Column(name = "estado")
private String estado;
@Column(name = "senha")
private String senha;
@Column(name = "data")
private String data;
@Column(name = "celular")
private String celular;
@Column(name = "cpf")
private String cpf;
@Column(name = "cidade")
private String cidade;
@Column(name = "cep")
private String cep;
@Column(name = "confirmar")
private String confirmar;
@Column(name = "rua")
private String rua;
@Column(name = "numero")
private Integer numero;
@Column(name = "bairro")
private String bairro;
@Column(name = "uf")
private String uf;
@Column(name = "login")
private String login;
@Column(name = "telefone")
private String telefone;
public ClienteWeb() {
}
public ClienteWeb(Integer codigo) {
this.codigo = codigo;
}
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSexo() {
return sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getCelular() {
return celular;
}
public void setCelular(String celular) {
this.celular = celular;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public String getConfirmar() {
return confirmar;
}
public void setConfirmar(String confirmar) {
this.confirmar = confirmar;
}
public String getRua() {
return rua;
}
public void setRua(String rua) {
this.rua = rua;
}
public Integer getNumero() {
return numero;
}
public void setNumero(Integer numero) {
this.numero = numero;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getUf() {
return uf;
}
public void setUf(String uf) {
this.uf = uf;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
@Override
public int hashCode() {
int hash = 0;
hash += (codigo != null ? codigo.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof ClienteWeb)) {
return false;
}
ClienteWeb other = (ClienteWeb) object;
if ((this.codigo == null && other.codigo != null) || (this.codigo != null && !this.codigo.equals(other.codigo))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Entity.ClienteWeb[ codigo=" + codigo + " ]";
}
}
My bean that makes contact with form jsf
@ManagedBean(name="cliente")
@SessionScoped
public class Cliente {
private final ClienteDAO dao= new ClienteDAO();
public String CadastroCliente(){
dao.cadastrarCliente();
RequestContext.getCurrentInstance().update("grow1");
FacesContext context=FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"","Cadastro Realizado com Sucesso"));
return "Contato.xhtml?faces-redirect=true";
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSexo() {
return sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getCelular() {
return celular;
}
public void setCelular(String celular) {
this.celular = celular;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public String getConfirmar() {
return confirmar;
}
public void setConfirmar(String confirmar) {
this.confirmar = confirmar;
}
public String getRua() {
return rua;
}
public void setRua(String rua) {
this.rua = rua;
}
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getUf() {
return uf;
}
public void setUf(String uf) {
this.uf = uf;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
}