List data in a dataTable

1

I believe this method of dao should not be correct.

DAO METHOD

public List <Campeonato> listarTodos() {

        try {
            manager.getTransaction().begin();
            Query query = manager.createNamedQuery("Campeonato.listarTodos", Campeonato.class);
            @SuppressWarnings("unchecked")
            List <Campeonato> campeonatos = query.getResultList();
            for (Campeonato campeonato : campeonatos) {
                campeonato.setId(campeonato.getId());
                campeonato.setNome(campeonato.getNome());
                campeonatos.add(campeonato);
                Iterator <Campeonato> iterator = campeonatos.iterator();
            while (iterator.hasNext()) {
                campeonato = iterator.next();
                }
            } 
            return campeonatos;
        } finally {
            manager.close();
        }
    }

MY BEAN

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import dao.CampeonatoDao;

import model.Campeonato;

@ManagedBean
@RequestScoped
public class CampeonatoBean {

    private Campeonato campeonato;
    private List <Campeonato> campeonatos;

    public CampeonatoBean() {

        campeonato = new Campeonato();
    }
    public void salvar() throws Exception {

        CampeonatoDao campeonatoDao = new CampeonatoDao();
        campeonatoDao.salvar(campeonato);
    }

    public void deletar() throws Exception {

        CampeonatoDao campeonatoDao = new CampeonatoDao();
        campeonatoDao.deletar(campeonato);
    }

    public List <Campeonato> listarCampeonatos()  {

        CampeonatoDao campeonatoDao = new CampeonatoDao();
         campeonatos = campeonatoDao.listarTodos();
         return campeonatos;
    }

    public Campeonato getCampeonato() {
        return campeonato;
    }

    public void setCampeonato(Campeonato campeonato) {
        this.campeonato = campeonato;
    }

    public List<Campeonato> getCampeonatos() {
        return campeonatos;
    }

    public void setCampeonatos(List<Campeonato> campeonatos) {
        this.campeonatos = campeonatos;
    }
}

XHTML

<h:form> 
<p:fieldset> <br/>

<p:panelGrid columns="3" > 
<p:outputLabel value="CAMPEONATO: " /> 
<p:inputText  value="#{campeonatoBean.campeonato.nome}" />
<p:commandButton ajax="false" value="SALVAR" action="#{campeonatoBean.salvar}"/>
</p:panelGrid> <br/>

<p:commandButton ajax="false" value="LISTAR" action="#{campeonatoBean.listarCampeonatos}" />

<p:dataTable value="#{campeonatoBean.campeonatos}" var="campeonato" resizableColumns="true" >
<f:facet name="header">
<p:outputLabel value="LISTA DE CAMPEONATOS" />
</f:facet>
<p:column headerText="ID" sortBy="#{campeonato.id}" >
<h:outputText value="#{campeonato.id}" />
</p:column>     
<p:column headerText="CAMPEONATO" sortBy="#{campeonato.nome}" >
<h:outputText value="#{campeonato.nome}" />
</p:column>     
<p:column >
<p:commandButton ajax="false" value="EXCLUIR" action="#{campeonatoBean}"/>          
</p:column>
</p:dataTable>

</p:fieldset>
</h:form>   

ERROR

Type Exception Report

Message java.util.ConcurrentModificationException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: java.util.ConcurrentModificationException
  javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

javax.faces.el.EvaluationException: java.util.ConcurrentModificationException
  javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
  com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
  javax.faces.component.UICommand.broadcast(UICommand.java:315)
  javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
  javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
  com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
  com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
  javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.util.ConcurrentModificationException
  java.util.ArrayList$Itr.checkForComodification(Unknown Source)
  java.util.ArrayList$Itr.next(Unknown Source)
  dao.CampeonatoDao.listarTodos(CampeonatoDao.java:62)
  controller.CampeonatoBean.listarCampeonatos(CampeonatoBean.java:38)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  java.lang.reflect.Method.invoke(Unknown Source)
  org.apache.el.parser.AstValue.invoke(AstValue.java:247)
  org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
  com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
  javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
  com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
  javax.faces.component.UICommand.broadcast(UICommand.java:315)
  javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
  javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
  com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
  com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
  javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
    
asked by anonymous 18.07.2017 / 15:27

1 answer

0

The reported error is the ConcurrentModificationException exception, which according to with documentation, free translation:

  

This exception can be thrown by methods that detected the modification   object when that modification is not allowed.

That is, you are changing a Campeonato object that is contained in the campeonatos list, in the listarTodos method.

For the result that expects the list returned by getResultList() is enough, it would look like:

public List <Campeonato> listarTodos() {
    try {
        manager.getTransaction().begin();
        Query query = manager.createNamedQuery("Campeonato.listarTodos", Campeonato.class);
        return query.getResultList();
    } finally {
        manager.close();
    }
}
    
18.07.2017 / 16:43