1

JPA と JSF を使用して単純なリストと編集ページを実装しようとしていますが、データベースに 4 つのレコードがあるにもかかわらず、リストは行を返しません。スタック トレースにエラーがないため、根本的な原因を見つけるのに苦労しています。とりあえず、init メソッドが呼び出されていないことがわかります。手を貸してくれませんか?

Usuario.java

package br.com.jpajsf.model.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Usuario {

@Id
@GeneratedValue
private int idUsuario;

private String dsIdentificador;

private String dsSenha;

private String dsSal;

private String dsNome;

private String dtNascimento;
// getters, setters, hashCode e equals.

UsuarioServices.java

package br.com.jpajsf.persistence;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import br.com.jpajsf.model.entity.Usuario;

@Stateless
public class UsuarioServices {
@PersistenceContext
private EntityManager em;

public List<Usuario> getUsuarios() {
    return em.createQuery("select u from Usuario u", Usuario.class)
            .getResultList();
}

public Usuario find(String id) {
    return em.find(Usuario.class, Integer.parseInt(id));
}

public void persist(Usuario u) {
    em.persist(u);
}
}

UsuarioBean.java

package br.com.jpajsf.domain;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.jpajsf.model.entity.Usuario;
import br.com.jpajsf.persistence.UsuarioServices;

@ViewScoped
@Named
public class UsuarioBean {
@Inject
private UsuarioServices usuarioServices;

private Usuario usuario;
private List<Usuario> usuarios;

@PostConstruct
public void init() {
    Map<String, String> params = FacesContext.getCurrentInstance()
            .getExternalContext().getRequestParameterMap();

    if (!params.isEmpty()) {
        try {
            usuario = usuarioServices.find(params.get("idUsuario"));
        } catch (NumberFormatException e) {
        }
    }

    if (usuario == null) {
        usuario = new Usuario();
    }

    setUsuarios();
}

public List<Usuario> getUsuarios() {
    if (usuarios == null) {
        setUsuarios();
    }
    return usuarios;
}

public void setUsuarios() {
    this.usuarios = usuarioServices.getUsuarios();
}

public Usuario getUsuario() {
    return usuario;
}

public void setUsuario(Usuario usuario) {
    this.usuario = usuario;
}

public String salvar(Usuario u) {
    usuarioServices.persist(u);
    return "listar";
}
}

listar.xhtml

<h:head>
    <title>Cadastro de usuários</title>
</h:head>
<h:body>
    <h:form>
        <h:dataTable value="#{usuarioBean.usuarios}" var="usuario" border="1">
            <h:column>
                <f:facet name="header">ID</f:facet>
                #{usuario.idUsuario}
            </h:column>
            <h:column>
                <f:facet name="header">Email</f:facet>
                #{usuario.dsIdentificador}
            </h:column>
            <h:column>
                <f:facet name="header">Senha</f:facet>
                #{usuario.dsSenha}
            </h:column>
            <h:column>
                <f:facet name="header">Sal</f:facet>
                #{usuario.dsSal}
            </h:column>
            <h:column>
                <f:facet name="header">Nome</f:facet>
                #{usuario.dsNome}
            </h:column>
            <h:column>
                <f:facet name="header">Data Nascimento</f:facet>
                #{usuario.dtNascimento}
            </h:column>
            <h:column>
                <f:facet name="header">Opções</f:facet>
                <h:commandLink value="editar" action="editar?faces-redirect=true&amp;includeViewParams=true">
                    <f:setPropertyActionListener target="#{usuarioBean.usuario.idUsuario}" value="#{usuario.idUsuario}"/>
                </h:commandLink>
            </h:column>
        </h:dataTable>
    </h:form>
</h:body>
</html>

もうありがとう!

4

2 に答える 2