List data in a datatable

Asked

Viewed 192 times

1

I believe this method of dao that 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.
  • What mistake or problem are you having?

  • Quando clico no botão listar... da esse erro&#xA;java.util.ConcurrentModificationException&#xA;&#xA;e no console mostra...&#xA;ADVERTÊNCIA: #{campeonatoBean.listarCampeonatos}: java.util.ConcurrentModificationException&#xA;javax.faces.FacesException: #{campeonatoBean.listarCampeonatos}: java.util.Concurrentmodificationexception

  • dao.CampeonatoDao.lista!

  • Post the error stacktrace in the question to facilitate

  • The problem is that you are making changes to the list while you are browsing it. Related: Concurrentmodificationexception how to proceed?

  • Excuse the little knowledge, but where would be the cause of the change? Because that is not the intention, I just want to list all championships in datatable!

Show 1 more comment

1 answer

0

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

This exception can be made by methods that detected the modification simultaneous of an object when such modification is not permitted.

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

For the result waiting for the list returned by getResultList() is enough, would be:

public List <Campeonato> listarTodos() {
    try {
        manager.getTransaction().begin();
        Query query = manager.createNamedQuery("Campeonato.listarTodos", Campeonato.class);
        return query.getResultList();
    } finally {
        manager.close();
    }
}
  • I will test here and give the feedback. Thank you very much!

  • Henrique Thank you so much! Thanks God! CONTINUE ALWAYS HELPING PEOPLE! WE NEED A LOT! VLW! HUG!

  • Just to close the understanding... did not need the foreach, because of the namedquery, would that neh?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.