5 Replies Latest reply on Mar 29, 2007 9:58 AM by hasc

    Question on Injecting

    hasc

      Hello,

      i got a question on how to inject a component.

      i've got a class that loads database objects.



      @Stateful
      @Name("ItemsCache")
      public class ItemsCacheBean implements ItemsCache, Serializable {
      
       @PersistenceContext
       EntityManager em;
      
       private List<MyObjects> items;
      
       @Create
       public void cacheObjects() {
       items = em.createQuery("select . from Object o")
       .setHint("org.hibernate.cacheable", true)
       .getResultList();
       }
      
       public List<MyObjects> getItemsList(){
       return items;
       }
      
       @Remove @Destroy
       public void destroy() {}
      }


      now in another bean i want to get the list of objects once, ant then serve it as listitems where the label of the displayed item depends on the locale whenever a clients requests it. i tried the following:



      @Stateful
      @Name("itemsmanager")
      public class ItemsManagerBean implements ItemsManager, Serializable {
      
       @In(create=true) ItemsCache itemscache;
      
       @In Locale locale;
      
       Map<String,MyObject> objectsMap;
      
       public Map<String,MyObject> getObjects()
       {
       List<MyObjects> items = itemscache.getItemsList();
      
       String locale = this.locale.toString();
      
       Map<String,MyObject> results = new TreeMap<String,MyObject>();
      
       for (MyObject myobject: items) {
       results.put(myobject.getName(locale),myobject);
       }
      
       objectsMap = results;
       return objectsMap;
       }
      
       public Converter getConverter()
       {
       return new MyObjectsTypeConverter(itemscache.getItemsList(););
       }
      
       @Remove @Destroy
       public void destroy() {}
      }



      regards,
      hasc

        • 1. Re: Question on Injecting
          waynebagguley

          What's the question?

          • 2. Re: Question on Injecting
            hasc

            the question is how do i inject the list correctly.

            I get the following error message:

            javax.el.ELException: /showitems.xhtml @17,54 value="#{itemsmanager.objects}": javax.ejb.EJBTransactionRolledbackException: org.jboss.seam.RequiredException: In attribute requires non-null value: itemsmanager.itemscache


            thanks for helping

            • 3. Re: Question on Injecting
              pmuir

              Try injecting ItemsCache not itemscache

              • 4. Re: Question on Injecting
                waynebagguley

                Ah yeah, well spotted pete.

                The name of the injected field has to match the declaration in @Name

                Change the code to:

                @Stateful
                @Name("itemsCache")
                public class ItemsCacheBean implements ItemsCache, Serializable {
                ...
                @Stateful
                @Name("itemsmanager")
                public class ItemsManagerBean implements ItemsManager, Serializable {
                
                 @In(create=true) ItemsCache itemsCache;
                



                • 5. Re: Question on Injecting
                  hasc

                  eh... that was a stupid question of me.

                  thanks a lot for help.