1 2 Previous Next 18 Replies Latest reply on Mar 18, 2008 2:19 PM by Uros Majeric

    list in application scope

    Uros Majeric Newbie

      Hay,


      I have a question about lists. I have been looking for solution but didn't find anything usefull.


      I have a table in database witch isn't changing at all (astrological signs) and I wan't to show a drop down on the page.
      Now the problem is that this list is retrieving from database every time I access page (with dropdown on it). How can I put this list in APPLICATION scope (or cache), so it will be available all the time during application and it won't be retrieved from database?



      Thanks for help.


      Uros

        • 1. Re: list in application scope
          Damian Harvey Apprentice

          Why don't you get rid of the table and make it a static list? That way you won't need the database access at all and won't be at risk of Lazy Init Exceptions or detached entities.


          Cheers,


          Damian.

          • 2. Re: list in application scope
            Uros Majeric Newbie

            But still what if I wan't to have this in database. Is there any solution?

            • 3. Re: list in application scope
              Nicklas Karlsson Master

              Use a query hint to use cache. Still better than having it in application scope, I think

              • 4. Re: list in application scope
                Carsten Hoehne Master

                Your task is very easy to achieve.
                Make a seam component in ApplicationScope.


                @Name(value ="astroList")
                
                @AutoCreate
                
                @Scope(value = ScopeType.APPLICATION)
                
                public class AstroListManager {
                
                
                     @SuppressWarnings("unchecked")
                
                     @Unwrap
                
                     public List<astroelem> astrolistFactory(){
                
                          return //yourlist;
                
                     }
                
                }
                
                
                


                Under the name astroList you have access to your List. The factory method will be only called if astroList is not set in any Context.
                The documentation for seam will explain this in detail.
                (Factory and Manager Components)
                If you have further question please do not hesitate to ask.


                Ciao,

                Carsten


                • 5. Re: list in application scope
                  Uros Majeric Newbie

                  I forgot to say that I have also some table that doesn't change verry often but it still changes. What about in this case?

                  • 6. Re: list in application scope
                    Nicklas Karlsson Master

                    The second level cache could be useful here, too. You could have the cache expired after 10 minutes or so (if it's ok that the change would come to effect at the next expire and not immediately).

                    • 7. Re: list in application scope
                      Carsten Hoehne Master

                      Look at my solution but set the scope to SESSION. That is what i have done in this case

                      • 8. Re: list in application scope
                        Uros Majeric Newbie

                        Thanks for your help.


                        Uros

                        • 9. Re: list in application scope
                          Damian Harvey Apprentice

                          And for extra value use @Observer to refresh parts of the list when an @Event is raised that changes some of the data. This way you don't have to refresh the whole list on every change, it only needs to refresh part of it.


                          Cheers,


                          Damian.

                          • 10. Re: list in application scope
                            Christian Bauer Master

                            Carsten Höhne wrote on Mar 07, 2008 11:24 AM:

                            The factory method will be only called if astroList is not set in any Context.


                            This is not correct, read the documentation of @Unwrap.

                            • 11. Re: list in application scope
                              Carsten Hoehne Master

                              As you see i have mixed the explanation of @Factory and @Unwrap
                              The quoted sentence is correct if it refers to a method annotated with @Factory.


                              With unwrap use the example in the docu (hens), which is quiet more correct than my short piece of code:-( And it solves this problem

                              • 12. Re: list in application scope
                                Uros Majeric Newbie

                                I did this now, but it doesn't work as it should. The problem is:
                                when first user accesses the page it works as it should, but when the seccond user accesses the page its sign isn't selected (list is positioned on 'Select your sign'). I implemented method on Signs


                                     public boolean equals(Object obj) {
                                          System.out.println("obj: "+obj+", this: "+this);
                                          if (obj instanceof Sign) {
                                               return (this.getId() == ((Sign)obj).getId());
                                          }
                                          return false;
                                     }


                                I tried to output what does it compares (System.out).
                                This is what it outputs on first access:


                                obj: null, this: (id: 1, name: Ram)
                                obj: (id: 1, name: Ram), this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)
                                obj: null, this: (id: 1, name: Ram)



                                And this outputs when I access the page seccond time:



                                obj: null, this: (id: 2, name: Bull)
                                obj: (id: 1, name: Ram), this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)
                                obj: null, this: (id: 2, name: Bull)



                                and the signs Factory class lokks like this:


                                @Name("signs")
                                @Scope(ScopeType.APPLICATION) 
                                public class Signs {
                                     @In private EntityManager entityManager;
                                     
                                     List<Sign> signs;
                                     
                                     @Unwrap
                                    public List<Sign> getSigns() {
                                        if (signs == null) {
                                            signs = entityManager.createQuery("FROM Sign").getResultList();
                                        }
                                        return signs;
                                    }
                                }



                                and the Facelets code:



                                <h:selectOneMenu value="#{currentUser.sign}">
                                     <s:selectItems value="#{signs}" var="sign" label="#{messages[sign.name]}" noSelectionLabel="#{messages.sign_selectionLabel}"/>
                                     <s:convertEntity/>
                                </h:selectOneMenu>




                                it looks like a bug to me.
                                Is it?


                                Uros

                                • 13. Re: list in application scope
                                  asaf sh Expert

                                  is your sign.id is primitive? make sure it is,


                                  otherwise, if your sign.id is an object Long/Integer than you have to use equals()



                                  Asaf.

                                  • 14. Re: list in application scope
                                    Uros Majeric Newbie

                                    sign.id type is int.
                                    I still don't know if this is a bug or I made a mistake?


                                    Uros

                                    1 2 Previous Next