0
I’m having problems, in the project I’m developing in jsf using jpa and Hibernate. I am not able to register the customer data in my database as I can relate 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 doubt is how I will put the values of the objects typed by the user that I pick up with my class of magnedBean client and put in the method cadastrarCliente, being that in it I persist the database of my class of entiade Clienteweb ?
My class of entity
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;
}
}
Sorry, I couldn’t quite understand, I’m trying to come up with an answer here. Help me, your class
ClienteWeb
is responsible for receiving the form data filled by the user? And you are not able to pass the class dataClienteWeb
for the classCliente
who is saved on the bench?– Paulo H. Hartmann
Brother, you are instantiating
Ciente cli = new Cliente()
and is setting the values ofcli
, that you just instantiated intocli
?– Henrique Santiago
Your goal would not be in montaCliente(), set the values you take from the cadster form?
– Henrique Santiago
ex:
cli.setCampo(campo);
– Henrique Santiago
send the form also that is better to help
– Henrique Santiago
so Henrique estou usando jsf , min ha dúvida é é como vou pegar os dados do jsf e persisti ele no banco de dados, sendo que no pesist tenho que colocar meu metodo de entidade Entity ?
– User1999
I added the magnedBean and entity classes for you to visualize better
– User1999
Man, the method
montaCliente()
should not exist. It makes no sense. In the answer I posted explain with the data are transferred from the form to the Managed bean. You got that part?– igventurelli