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

    Question on Injecting

    Hannes Schulz Newbie

      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