6 Replies Latest reply on Jan 21, 2011 4:49 PM by ilya40umov

    suggestionBox triggeres Update

    digdas

      I created a suggestion box:
      <s:decorate id="nameDecoration" template="layout/edit.xhtml">
      <ui:define name="label">NameX</ui:define>
      <h:inputText id="name" required="true" value="#{testdataHome.instance.name}"/>
      <rich:suggestionbox
      id="suggestionBoxId"
      for="name"
      suggestionAction="#{testBean.autocomplete}"
      var="res"
      ajaxSingle="true" bypassUpdates="true"
      process="name" selfRendered="true">
      When I type one character in the suggestion box, it triggeres an SQL-update on the testdataHome-object. If I change:
      <h:inputText id="name" required="true" value="#{testdataHome.instance.name}"/>
      to
      <h:inputText id="name" required="true" value="fixedvalue"/>
      it does not.

      I use a default Seam war (using 2.0.2GA), created a testdata seam entity and put a testBean bean in the actions, which produces a short list.

      What am I doing wrong here?

        • 1. Re: suggestionBox triggeres Update
          ilya_shaikovsky

          Input text gets processed while calling suggestion and its works like designed, because we need to get the value from input.

          • 2. Re: suggestionBox triggeres Update
            digdas

            Thanks. What I am looking for is that the data from the suggestionBox is only put into the inputText-box. The update (I see hibernate-SQL update in the logging) should only be done when clicking Save...

            I imagined that the suggestionBox would do this also.

            How to do this?

            • 3. Re: suggestionBox triggeres Update
              hdu

              Hello, has this question been answered? I am also having the same problem with the database being updated automatically. Ideally it should update the entity bean property that the value is bound to but not the update database until I save it.

              • 4. Re: suggestionBox triggeres Update
                ilya40umov

                I guess this can unswer your question:

                http://seamframework.org/Community/SeamUpdatesEntitiesAutomaticallyViaUpdateTimestampsCache

                http://seamframework.org/Community/EntityManagerSingleEntityUpdate

                So you should make your entity detached in order to save it manually and then use merge.

                • 5. Re: suggestionBox triggeres Update
                  hdu

                  Thanks for the links, Ilya.

                   

                  I've been thinking about how I can get around this problem of premature update and am still very stuck. On my page there are some fields that use the rich:suggestionbox and for which the input value is bound to a Seam managed entity. So whenever I change the value in the suggestionbox, it gets updated automatically in the database. I've thought about several solutions to this problem:

                   

                  1) Clone a copy of the managed entity. This clone would then be detached which I could bind to the suggestionbox instead of the managed entity. Cloning would be relatively simple if it was just a surface copy, but I need a deep copy since my entity has associations to other entities which are also displayed on the page and can also be edited. This would make cloning a nightmare and which I hope to avoid.

                   

                  2) Detach the managed entity using Seam. Originally I thought this would be quite simple, but apparently in Seam managed persistence, there is only the enityManager.clear() method which detaches ALL its managed entities which is undesirable. There does not seem to be a way to just detach an entity in Seam, unless I am missing something.

                   

                  3) Detach the managed entity using Hibernate. I could use session.evict() to detach the entity, but then I run into the LazyInitializationException when the page tries to access the associated entities.

                   

                  Is there any other way to detach the entity?

                  • 6. Re: suggestionBox triggeres Update
                    ilya40umov

                    1) Cloning is not the best way. I guess if you will choose it you should better try to use DTOs.

                    2) I know that JPA 1.x enity manager can't do detaching enities. This thing is available in JPA 2.0(EJB 3.1). So definetly you can't use it if you are not using EJB 3.1.

                    3) The best way to workaround LazyInitializationException is to preload all needed data before detaching entity. And then just use it. You can also try Hibernate.initialize method. It's tricky and even hucky I think. But it works fine.

                     

                    P.S. This is why I'm not using Seam. Such a complicated and comfortable Java framevork you are trying to use such much more pain you will have in the future with bugs and performance. Because you can't simply change anything in the framework because it's too complicated and finally your code will look like a big workaround. =)

                    P.S.S. Like an every J2EE developer I like pain all these new things and one day I'll definetly try Seam in my application.