Order By Hibernate in JSF

0

Hello, good afternoon, I'm a beginner in this web world and I need to order establishments by punctuation (the best rated in the beginning, LOGIC)

The error is An Error Occurred: java.lang.NullPointerException

Here is my entity, any help is welcome

public class Estabelecimento implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Integer id;
@Column
String nome;
String cnpj;
String cidade;
String bairro;
String rua;
String numero;
String telefone;
int pontos;

Here is the Bean code

public List listaEstabelecimentosPontos(){
  listaEstabelecimentos =  estabelecimentoDao.ordenarPontos();
  return this.listaEstabelecimentos;
}

And here is the xml

<?xml version="1.0" encoding="UTF-8"?>

<ui:define name="content">
    <h:form>
        <p:dataTable var="tabela" value="#{estabelecimentosBean.listaEstabelecimentosPontos()}">
        <p:column headerText="Id">
            <h:outputText value="#{tabela.id}" />
        </p:column>

        <p:column headerText="nome">
            <h:outputText value="#{tabela.nome}" />
        </p:column>

        <p:column headerText="cnpj">
            <h:outputText value="#{tabela.cnpj}" />
        </p:column>

        <p:column headerText="cidade">
            <h:outputText value="#{tabela.cidade}" />
        </p:column>

        <p:column headerText="bairro">
            <h:outputText value="#{tabela.bairro}" />
        </p:column>

        <p:column headerText="rua">
            <h:outputText value="#{tabela.rua}" />
        </p:column>

        <p:column headerText="numero">
            <h:outputText value="#{tabela.numero}" />
        </p:column>

        <p:column headerText="telefone">
            <h:outputText value="#{tabela.telefone}" />
        </p:column>

        <p:column headerText="AÇÕES">
              <center>
              <p:commandButton value="Remover" action="#{estabelecimentosBean.removerEstabelecimento(tabela)}" />
              <p:commandButton value="Editar" action="#{estabelecimentosBean.atualizarEstabelecimento(tabela)}" />
              </center>
        </p:column>

     </p:dataTable>
  </h:form>
</ui:define>

Error Log

java.lang.NullPointerException
at br.com.fastchoice.DAO.EstabelecimentoDAO.ordenarPontos(EstabelecimentoDAO.java:37)
at br.com.fastchoice.bean.EstabelecimentosBean.listaEstabelecimentosPontos(EstabelecimentosBean.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:183)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
at org.apache.el.parser.AstValue.getValue(AstValue.java:173)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:732)
at org.primefaces.component.api.UIData.getDataModel(UIData.java:759)
at javax.faces.component.UIData.getRowCount(UIData.java:356)
at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:806)
at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:788)
at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:281)
at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:243)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85)
at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:72)
at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

DAO Class

public class EstabelecimentoDAO {

Session sessao;
Transaction trans;
List<Estabelecimento> list;

public List<Estabelecimento> getList() {
    sessao = HibernateUtil.getSessionFactory().openSession();
    trans = sessao.beginTransaction();
    Criteria cri = sessao.createCriteria(Estabelecimento.class);
    this.list = cri.list();
    return list;

}

public List<Estabelecimento> ordenarPontos() {
    String jpql = "SELECT * FROM Estabelecimento estabelecimento ORDER BY estabelecimento.pontos DESC";
    return sessao.createQuery(jpql).getResultList();
}/**/


public void adicionarEstabelecimento(Estabelecimento es) {
    try {
        sessao = HibernateUtil.getSessionFactory().openSession();
        trans = sessao.beginTransaction();

        Estabelecimento estabelecimento = new Estabelecimento();
        estabelecimento.setCnpj(es.getCnpj());
        estabelecimento.setNome(es.getNome());

        estabelecimento.setCidade(es.getCidade());
        estabelecimento.setBairro(es.getBairro());
        estabelecimento.setRua(es.getRua());
        estabelecimento.setNumero(es.getNumero());
        estabelecimento.setTelefone(es.getTelefone());
        estabelecimento.setPontos(es.getPontos());
        sessao.save(estabelecimento);
        trans.commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sessao.close();
    }
}

public void removerEstabelecimento(Estabelecimento es) {

    try {
        sessao = HibernateUtil.getSessionFactory().openSession();
        trans = sessao.beginTransaction();

        sessao.delete(es);
        trans.commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sessao.close();
    }

}

public void editarEstabelecimento(Estabelecimento es) {

    try {
        sessao = HibernateUtil.getSessionFactory().openSession();
        trans = sessao.beginTransaction();

        sessao.update(es);
        trans.commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sessao.close();
    }

}

}

    
asked by anonymous 05.11.2015 / 15:31

1 answer

0

Well, here in this snippet of code:

public List<Estabelecimento> ordenarPontos() {
    String jpql = "SELECT * FROM estabelecimentos ORDER BY pontos DESC";
    return sessao.createQuery(jpql).list();
}

You are doing a parameter search in the wrong way.

The correct one would be:

public List<Estabelecimento> ordenarPontos() {
    sessao = HibernateUtil.getSessionFactory().openSession();
    trans = sessao.beginTransaction();
    String jpql = "SELECT estabelecimento FROM Estabelecimento estabelecimento ORDER BY estabelecimento.pontos DESC";
    return sessao.createQuery(jpql).list();
}

I hope I have helped.

Abs

    
05.11.2015 / 18:17