0
I need help saving the data of an application using JPA. I followed a tutorial that only helped in the implementation of login but did not show how to save the data. In this case, I am trying to register a user. When I click save it shows an error message that I believe to be for lack of some implementation:
javax.el.PropertyNotFoundException: /cadUsuario.xhtml @22,136 value="#{UsuarioMB.usuario.usuario}": Target Unreachable, identifier 'UsuarioMB' resolved to null
User class
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "USUARIOS")
//@NamedQuery(name="query", query="SELECT c FROM Cliente c")
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private long id;
@Column(name = "USUARIO", length = 10, nullable = false)
private String usuario;
@Column(name = "SENHA", length = 5, nullable = false)
private String senha;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}
User Class
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import model.Usuario;
public class UsuarioDAO {
private EntityManagerFactory factory = Persistence
.createEntityManagerFactory("usuarios");
private EntityManager em = factory.createEntityManager();
public Usuario getUsuario(String usuario, String senha) {
try {
Usuario usu = (Usuario) em
.createQuery(
"SELECT u from Usuario u where u.usuario = :usuario and u.senha = :senha")
.setParameter("usuario", usuario)
.setParameter("senha", senha).getSingleResult();
return usu;
} catch (NoResultException e) {
return null;
}
}
public boolean inserirUsuario(Usuario usuario) {
try {
em.persist(usuario);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean deletarUsuario(Usuario usuario) {
try {
em.remove(usuario);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
Class Usuariomb
import daos.UsuarioDAO;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import model.Usuario;
@ManagedBean
@SessionScoped
public class UsuarioMB {
private Usuario usuario;
public UsuarioMB() {
}
public UsuarioMB(Usuario usuario) {
this.usuario = usuario;
}
public Usuario getUsuario() {
return usuario;
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
}
registration page in the case
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Lavagem Godoi</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
</h:head>
<ui:include src="menu/menu.xhtml" />
<h:body>
<h:form>
<p:growl id="msgs"/>
<p:messages id="messages" />
<p:fieldset legend="Cadastro de Usuário" style="margin-bottom:20px">
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel for="id" value="Id" />
<p:inputText id="id" value="#{UsuarioMB.usuario.id}" disabled="true" />
<p:outputLabel for="usuario" value="Nome do Usuário:" />
<p:inputText id="usuario" value="#{UsuarioMB.usuario.usuario}" required="true" requiredMessage="Insira o usuário"/>
<p:outputLabel for="senha" value="Senha:" />
<p:password id="senha" value="#{UsuarioMB.usuario.senha}" required="true" requiredMessage="Insira a senha"/>
<p:commandButton value="Salvar" action="#{UsuarioDAO.inserirUsuario}" > </p:commandButton>
</h:panelGrid>
</p:fieldset>
</h:form>
</h:body>
</html>
Your solution solved this problem. However appeared another that should questioned in another question. Anyway thank you.
– Eduardo Krakhecke
@Eduardokrakhecke you saw my answer?
– igventurelli
yes. that question I managed to resolve.
– Eduardo Krakhecke