4 Replies Latest reply on Oct 2, 2008 9:50 AM by Chris C

    Should I use EntityHome at all??

    Chris C Newbie
      We've been using the EntityHome for our Seam CRUDs and have noticed that any calls out from the JSF page to the backing bean causes the instance to automatically get persisted to the database. This happens even if we don't call persist() or update() methods on EntityHome.

      Because of this, our Ajax sections cause unintentional persistence. Whenever a field is set on the entity, even before the user hits save button on the form, our entity gets saved. This is a really bad side effect for us. There are also performance concerns if we are going to the database to persist things too often.

      Is there a way to turn off this automatic persistence in EntityHome and only save when persist() or update() are called?

      PS. We are using Seam version 2.0.2.SP1

      Below is an extract of the Ajax section that talks to EntityHome:

      <a4j:region>
      <s:decorate id="levelDecoration" template="layout/edit.xhtml" >
          <ui:define name="label">Level</ui:define>
          <h:selectOneMenu id="level" required="true" value="#environmentalCertificateHome.instance.level}">
            <f:selectItem
              itemValue="State"
              itemLabel="State"/>
            <f:selectItem
              itemValue="Federal"
              itemLabel="Federal"/>
            <a4j:support event="onchange" reRender="state"></a4j:support>
           </h:selectOneMenu>
      </s:decorate>
      </a4j:region>

      <s:decorate id="stateDecoration" template="layout/edit.xhtml" >
          <ui:define name="label">State</ui:define>
          <h:selectOneMenu id="state"
             required="true" 
             value="#{environmentalCertificateHome.instance.state}" disabled="#{environmentalCertificateHome.instance.level=='Federal'}">
             <s:selectItems var="state" value="#{states}"
               label="#{state.description}"
                itemValue="#{state.value}"/>
            <f:selectItem
              itemValue="N/A"
              itemLabel="Not Applicable" />
          </h:selectOneMenu>                 
      </s:decorate>


      Thanks,
      Chris.
        • 1. Re: Should I use EntityHome at all??
          Tomasz Wilczynski Newbie

          EntityManager used by EntityHome probably uses FlushModeType.AUTO by default. If you were using the MANUAL mode, the data would be written to database only after calling entityManager.flush() (in your case getEntityManager().flush()). So what you need to do is add getEntityManager().setFlushMode( FlushModeType.MANUAL ) (or getPersistenceContext().setFlushMode( FlushModeType.MANUAL )). I think it would be ok to put this into constructor because I doubt the entityManager gets injected by the interceptor in case of EntityHome.

          • 2. Re: Should I use EntityHome at all??
            Ingo Jobling Master

            Another way to resolve this is to add the following to the page descriptor (page.xml)


            <begin-conversation flush-mode="MANUAL" join="true" />
            



            • 3. Re: Should I use EntityHome at all??
              Chris C Newbie
              Thanks Tomasz. Even though for some strange reason MANUAL was not supported as a JPA supported option, your idea to change the flush mode was spot on. We instead used the seam annotation in our seam scope to switch the flush mode to manual in our EntityHome.

                  @Begin(flushMode=FlushModeType.MANUAL)
                  public void create() {
                      super.create();
                  }


              Looks like this has done the trick and our JSF is no longer causing persists to the database until we have properly flushed.

              Very clever.

              Thanks,
              Chris.

              • 4. Re: Should I use EntityHome at all??
                Chris C Newbie

                At the moment, we prefer to keep the flush mode setting within the EntityHome itself, but knowing this other way to set the flush mode to MANUAL should come in pretty handy for the future.


                Thanks Ingo.