3 Replies Latest reply on Apr 20, 2011 12:25 PM by Leo van den berg

    OptimisticLockException - Edit DataTable with Modal Panel

    H G Newbie

      Hi,


      I'm trying to display data in dataTable. This data is editable trough Model panel.
      The problem appear when I'try to edit a row for seccond time and save the changes, always receive javax.persistence.OptimisticLockException.


      I'm using Seam 2.1.2, JBoss AS 5.1, MySQL.


      VIEW



      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
          xmlns:s="http://jboss.com/products/seam/taglib"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:rich="http://richfaces.org/rich"
          xmlns:a="http://richfaces.org/a4j"
          template="layout/maintemplate.xhtml">
      
      <ui:define name="body">
       <a:form id="fEditUser">
        <rich:dataTable id="dtEditUser"
                           var="webuser"
                        ajaxKeys="#{editUser.keys}"
                        value="#{webusers}"
                        rows="15">
         <rich:column>
          <f:facet name="header">Nombre de usuario</f:facet>
           <h:outputText value="#{webuser.userName}" /> 
         </rich:column>
         <rich:column>
          <f:facet name="header">
           <h:outputText value="Editar" />
          </f:facet>
          <a:commandLink value="editar"
                      onclick="#{rich:component('mpEditUser')}.show()">
            <f:setPropertyActionListener value="#{webuser}"
                                target="#{editUser.currentRowx}" />
           </a:commandLink>
         </rich:column>
        </rich:dataTable> 
       </a:form>
           
       <rich:modalPanel id="mpEditUser" autosized="true" width="450">
        <f:facet name="header">
         <h:outputText value="Editar usuario" />
        </f:facet>
        <f:facet name="controls">
         <h:panelGroup>
          <h:graphicImage value="/img/modal/close.png" id="hidelink"
                          styleClass="hidelink" />
          <rich:componentControl for="mpEditUser" attachTo="hidelink"
                                 operation="hide" event="onclick" />
         </h:panelGroup>
        </f:facet>
        <h:form>
         <rich:messages style="color:red;"></rich:messages>
          <h:panelGrid columns="1">
           <a:outputPanel ajaxRendered="true">
             <h:panelGrid columns="2">
              <h:outputText value="Nombre" />
              <h:inputText value="#{editUser.currentRowx.name}" />
             </h:panelGrid>
            </a:outputPanel>
            <a:commandButton value="Guardar"
                             action="#{editUser.updateRow}"
                             reRender="fEditUser"
                             oncomplete="if (#{facesContext.maximumSeverity==null}) #{rich:component('mpEditUser')}.hide();" />
         </h:panelGrid>
        </h:form>
       </rich:modalPanel>
      </ui:define>
      </ui:composition>




      SEAM component



      @Name("editUser")
      @Scope(ScopeType.PAGE)
      public class EditUser
      {
      
           @In private EntityManager entityManager;
           @Out(required = false)
           private List <User> webusers;
           @Out(required = false)
           private Set <Integer>keys;
           @Out(required = false)
           private User currentRowx;
      
           
           @Factory("webusers")
           public void getWebUsers() {
                try {
                     webusers = entityManager.createQuery("from gabino.addcel.entity.User u where u.roll=1 and u.active=true order by u.userName ASC")
                     .getResultList();
                } catch(Exception e) {
                     System.out.println("webusers:" + e.toString());
                }
           }
           @Transactional
           public void updateRow(){
                try {
                     if(getCurrentRowx() != null) {
                          entityManager.merge(getCurrentRowx());
                          entityManager.flush();
                          //int index = webusers.indexOf(currentRow);
                          //keys.add(index);
                     }
                } catch(Exception e) {
                     System.out.println("updateRow EditUser:" + e.toString());
                }
           }
      
           public Set<Integer> getKeys() {
                return keys;
           }
      
           public void setKeys(Set<Integer> keys) {
                this.keys = keys;
           }
           public User getCurrentRowx() {
                return currentRowx;
           }
           public void setCurrentRowx(User currentRowx) {
                this.currentRowx = currentRowx;
           }
      }




      persitence.xml




      <?xml version="1.0" encoding="UTF-8"?>
      <!-- Persistence deployment descriptor for dev profile -->
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
                   version="1.0">
                   
         <persistence-unit name="project_name" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/project_nameDatasource</jta-data-source>
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="update"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
               <!-- Only relevant if Seam is loading the persistence unit (Java SE bootstrap) -->
               <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/project_name_v0_3EntityManagerFactory"/>
            </properties>
         </persistence-unit>
          
      </persistence>




      components.xml



      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/components"
                  xmlns:core="http://jboss.com/products/seam/core"
                  xmlns:persistence="http://jboss.com/products/seam/persistence"
                  xmlns:drools="http://jboss.com/products/seam/drools"
                  xmlns:bpm="http://jboss.com/products/seam/bpm"
                  xmlns:security="http://jboss.com/products/seam/security"
                  xmlns:mail="http://jboss.com/products/seam/mail"
                  xmlns:web="http://jboss.com/products/seam/web"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation=
                      "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
                       http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd
                       http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.1.xsd
                       http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.1.xsd
                       http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd
                       http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.1.xsd
                       http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.1.xsd
                       http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd">
      
         <core:init debug="true" jndi-pattern="@jndiPattern@"/>
      
         <core:manager concurrent-request-timeout="500"
                       conversation-timeout="120000"
                       conversation-id-parameter="cid"
                       parent-conversation-id-parameter="pid"/>
      
         <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
         <web:hot-deploy-filter url-pattern="*.seam"/>
      
         <persistence:entity-manager-factory name="entityManagerFactory"
                            persistence-unit-name="project_name"
                            installed="false"/>
      
      
         <persistence:managed-persistence-context name="entityManager" auto-create="true"
                                entity-manager-factory="#{entityManagerFactory}"
                                persistence-unit-jndi-name="java:/project_name_v0_3EntityManagerFactory"/>
      
         <drools:rule-base name="securityRules">
            <drools:rule-files><value>/security.drl</value></drools:rule-files>
         </drools:rule-base>
      
         <security:rule-based-permission-resolver security-rules="#{securityRules}"/>
      
         <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
      
         <event type="org.jboss.seam.security.notLoggedIn">
            <action execute="#{redirect.captureCurrentView}"/>
         </event>
         <event type="org.jboss.seam.security.loginSuccessful">
            <action execute="#{redirect.returnToCapturedView}"/>
         </event>
      
         <mail:mail-session host="localhost" port="25"/>
      
         <!-- For use with jBPM pageflow or process management -->
         <!--
         <bpm:jbpm>
            <bpm:process-definitions></bpm:process-definitions>
            <bpm:pageflow-definitions></bpm:pageflow-definitions>
         </bpm:jbpm>
         -->
      
      </components>