EntityManager query.getResultList()返回空列表

时间:2016-02-06 18:17:08

标签: hibernate jpa

我是Hibernate和JPA的初学者,我试图通过List EntityManager从我的数据库中检索query.getResultList()项,但它不会返回任何内容。这是我搜索数据的bean:`

    import java.util.List;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    import javax.persistence.EntityManager;
    import javax.persistence.TypedQuery;
    import com.algaworks.financeiro.model.Lancamento;
    import com.algaworks.financeiro.util.JpaUtil;

    @ManagedBean
    @ViewScoped
    public class ConsultaLancamentosBean 
    {
       private List<Lancamento> lancamentos;
       public void consultar() 
      {
        EntityManager manager = JpaUtil.getEntityManager();
        TypedQuery<Lancamento> query = manager.createQuery
       (
         "from Lancamento", Lancamento.class
       );
       this.lancamentos = query.getResultList();
       manager.close();
     }
     public List<Lancamento> getLancamentos() 
     {
       return lancamentos;
     }
  }

搜索bean的视图:`

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:h="http://xmlns.jcp.org/jsf/html">
   <h:head>
      <title>Consulta de lançamentos</title>
   </h:head>
   <h:body>
      <f:metadata>
         <f:viewAction action="#{consultaLancamentosBean.consultar}" />
      </f:metadata>
      <h1>Consulta de lançamentos</h1>
      <h:form id="frm">
         <h:dataTable value="#{consultaLancamentosBean.lancamentos}"
            var="lancamento" border="1" cellspacing="0"
            cellpadding="2">
            <h:column>
               <f:facet name="header">
                  <h:outputText value="Pessoa"/>
               </f:facet>
               <h:outputText value="#{lancamento.pessoa.nome}"/>
            </h:column>
            <h:column>
               <f:facet name="header">
                  <h:outputText value="Descrição"/>
               </f:facet>
               <h:outputText value="#{lancamento.descricao}"/>
            </h:column>
            <h:column>
               <f:facet name="header">
                  <h:outputText value="Tipo"/>
               </f:facet>
               <h:outputText value="#{lancamento.tipo}"/>
            </h:column>
            <h:column>
               <f:facet name="header">
                  <h:outputText value="Valor"/>
               </f:facet>
               <h:outputText value="#{lancamento.valor}"/>
            </h:column>
            <h:column>
               <f:facet name="header">
                  <h:outputText value="Data de vencimento"/>
               </f:facet>
               <h:outputText value="#{lancamento.dataVencimento}"/>
            </h:column>
            <h:column>
               <f:facet name="header">
                  <h:outputText value="Data de pagamento"/>
               </f:facet>
               <h:outputText value="#{lancamento.dataPagamento}"/>
            </h:column>
         </h:dataTable>
      </h:form>
   </h:body>
</html>

getLancamentos()返回一个空列表,有人知道为什么会这样吗?提前致谢

更新: 我已经验证连接的所有内容都是正确的,并且所有表都有数据,我将bean更改为只有一个方法:`

public List<Lancamento> getLancamentos() 
{   
  EntityManager manager = JpaUtil.getEntityManager();
  TypedQuery<Lancamento> query = manager.createQuery("SELECT lan FROM Lancamento    lan", Lancamento.class);            
  List<Lancamento> lancamentos;
  lancamentos = query.getResultList();
  return lancamentos;
 }

And my new view is:

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
  <title>The size is:</title>
  <h:outputLabel value="#{consultaLancamentosBean.lancamentos}"> </h:outputLabel>
 </html>

`

但是现在,当我尝试运行它显示的代码时: HTTP状态500 - 无法初始化类 com.algaworks.financeiro.util.JpaUtil

我已导入此课程

1 个答案:

答案 0 :(得分:0)

试试这个:

TypedQuery<Lancamento> query = manager.createQuery("SELECT lan FROM Lancamento lan", Lancamento.class);
lancamentos = query.getResultList();

您的查询现在指定了SELECT

的内容