1
resposta

Erro quando código comentado no xhtml

Quando deixo o p:commandLink comentado para incluir o p:commandButton e usar FontAwesome acontece o erro abaixo

<p:dataList value="#{autorBean.autores}" var="autor" type="definition">
                    <f:facet name="header">
                        Autores
                    </f:facet>

                    <!-- <p:commandLink styleClass="ui-icon ui-icon-pencil" style="float:left;margin-right:3px" update=":autor">
                        <f:setPropertyActionListener value="#{autor}" target="#{autorBean.autor}" />                    
                    </p:commandLink>

                    <p:commandLink styleClass="ui-icon ui-icon-trash" style="float:left;margin-right:3px" update="@form" 
                                    action="#{autorBean.remover(autor)}" />
                    -->                
                    <p:commandButton icon="fa fa-fw fa-edit" update=":autor">
                        <f:setPropertyActionListener value="#{autor}" target="#{autorBean.autor}" />                    
                    </p:commandButton>

                    <p:commandButton icon="fa fa-fw fa-remove" update="@form" 
                                    action="#{autorBean.remover(autor)}" />

                    #{autor.nome} - #{autor.email}
                </p:dataList>
Exception

javax.servlet.ServletException: Error while committing the transaction
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

javax.persistence.RollbackException: Error while committing the transaction
    org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
    br.com.caelum.livraria.dao.DAO.remove(DAO.java:40)
    br.com.caelum.livraria.bean.AutorBean.remover(AutorBean.java:49)
    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)
    javax.el.BeanELResolver.invoke(BeanELResolver.java:158)
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
    org.apache.el.parser.AstValue.getValue(AstValue.java:159)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
    com.sun.faces.facelets.el.ELText$ELTextVariable.toString(ELText.java:228)
    com.sun.faces.facelets.el.ELText$ELTextComposite.toString(ELText.java:161)
    com.sun.faces.facelets.compiler.CommentInstruction.write(CommentInstruction.java:77)
    com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:83)
    org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:73)
    org.primefaces.component.datalist.DataListRenderer.encodeStrictList(DataListRenderer.java:186)
    org.primefaces.component.datalist.DataListRenderer.encodeMarkup(DataListRenderer.java:102)
    org.primefaces.component.datalist.DataListRenderer.encodeEnd(DataListRenderer.java:52)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`livrariadb`.`livro_autor`, CONSTRAINT `FK9590997AD70C7E4A` FOREIGN KEY (`autores_id`) REFERENCES `autor` (`id`))
    org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1377)
    org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
    org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:80)

Por que o código comentado acaba por chamar o método remover do autorBean?

1 resposta

Fala ai Flavio, tudo bem ?

Isso tem haver com o ciclo de vida do jsf, ele passa pelos componentes algumas vezes, você pode fazer alguma validação, ver se o usuário é diferente de nulo talvez.