4 Replies Latest reply on Aug 19, 2009 3:49 AM by Roy Abu Bakar

    using suggestionBox with live data

    Arbi Sookazian Master

      RF 3.3.1.GA

      There is a very good example on how to use rich:suggestionBox in Practical RF book. The example uses static in-memory data for all 50 states (i.e. no RDBMS table read).

      I have implemented my suggestionBox functionality by pre-loading the entity classes in my conversation-scoped SFSB Seam component as follows:

      //load all RecoveredEquipmentManagement entities into SMPC; assuming that siteId dropdown value is not populated so we can't add a filter to the JPQL query....
       public void init(){
       allRecoveredEquipmentManagementList = entityManager.createQuery("select rem from RecoveredEquipmentManagement rem").getResultList();

      I used this approach so I don't have to use <a4j:support event="onkeyup" action="#{foo.bar}"/> to make a db read (reduce db round-trips!) every time user enters a character in the inputText field.

      The concern I have with my approach is that it's possible to load thousands of entities into the SMPC (1st level cache) which is generally speaking not recommended as each entity is copied into the cache for dirty checking purposes. We don't want the cache to get too big!

      So what is the recommended approach here? Should I use a Hibernate StatelessSession? Or simply write
      after the
      b/c those entities don't need to be managed as they are used for dynamic report generation purposes only.

      A stateless session does not implement a first-level cache nor interact with any second-level cache, nor does it implement transactional write-behind or automatic dirty checking, nor do operations cascade to associated instances.


        • 1. Re: using suggestionBox with live data
          Nick Belaevski Master


          StatelessSession has some significant disadvantages, so why not create DTO objects serving for displaying purposes only and do it just from the query? http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql-select.html - it should be possible to do something like this:

          select new FamilyDTO(mother, mate, offspr)
          from DomesticCat as mother
           join mother.mate as mate
           left join mother.kittens as offspr

          where FamilyDTO is not persistent entity, thus not being put into SMPC.

          • 2. Re: using suggestionBox with live data
            Arbi Sookazian Master

            Thx for the fast response! I will try that out.

            I checked out the booking app from Seam 2.1.2.GA and he's using this:


            <h:inputText id="searchString" value="#{hotelSearch.searchString}" style="width: 165px;">
             <a:support id="onkeyup" event="onkeyup" actionListener="#{hotelSearch.find}" reRender="searchResults" />

            backing bean:

            public void find()
             page = 0;
            private void queryHotels() {
             List<Hotel> results = em.createQuery("select h from Hotel h where lower(h.name) like #{pattern} or lower(h.city) like #{pattern} or lower(h.zip) like #{pattern} or lower(h.address) like #{pattern}")
             .setFirstResult(page * pageSize)
             nextPageAvailable = results.size() > pageSize;
             if (nextPageAvailable)
             hotels = new ArrayList<Hotel>(results.subList(0,pageSize));
             } else {
             hotels = results;

            This is not a suggestionBox component but it's similar in terms of onkeyup event listener. He's doing a dynamic query for each onkeyup event! Maybe if you know the resultset is small that's ok but perhaps this is not the best example. (Also, they're still not using SMPC in this booking example!)

            Right now we have 300 rows in that table. Not sure how big it will grow. Thx again.

            • 3. Re: using suggestionBox with live data
              Arbi Sookazian Master

              On pg. 538 of JPA w/ Hibernate book, I read the following:

              If you have to create a few hundred or thousand objects in a unit of work, you may run into memory exhaustion. Every object that is passed to insert() or persist() is added to the persistence context cache. A straightforward solution is to flush and clear the persistence context after a certain number of objects.

              So perhaps in my code I can simply write
              after allRecoveredEquipmentManagementList has been populated by the JPQL createQuery() call.

              I'm not sure the DTO approach will work b/c I need entities in the List so the <s:convertEntity> tag will work.

              • 4. Re: using suggestionBox with live data
                Roy Abu Bakar Newbie


                Can you post your SFSB Seam component Class?