4 Replies Latest reply on Oct 2, 2006 11:12 AM by rlhr

    Populate pojoCache from Database at startup

      Hello,

      I created a component that is initialized at startup.
      This component is supposed to retrieve data from the database and store them in the pojoCache.

      The component is defined as:

      @Scope(ScopeType.APPLICATION)
      @Intercept(NEVER)
      @Startup(depends = {"pojoCache"})
      @Name("myComponent")
      public class MyComponent {
      
       privatePojoCache pojoCache = org.jboss.seam.core.PojoCache.instance();
      
       private EntityManager entityManager;
      
       @Create
       public void startup() throws Exception {
      
       // get data and store in the cache
       }
      }
      


      The entityManager that is also a component defined in the component.xml file as:

      <component name="entityManager"
      class="org.jboss.seam.core.ManagedPersistenceContext">
       <property name="persistenceUnitJndiName">java:/EntityManagerFactories/myDB</property>
      </component>
      


      To make sure the entityManager is loaded, I guess I need to add in the startup dependencies "entityManager", but then how do I get an instance of entityManager at this point?

      Injection is not working at startup.

      Thanks,

      Richard

        • 1. Re: Populate pojoCache from Database at startup
          pmuir

          If you just need a normal EJB entity manager (as opposed to a Seam Managed one) you can use JNDI to look up an EntityManager(Factory).

          • 2. Re: Populate pojoCache from Database at startup

            This is the entityManager I use in the application.
            Usually I can inject it using the component name, but in this case, since I have the annotation @Intercept(NEVER), I can't do that.

            Actually getting the component with a JNDI look up should work.
            I didn't even think of it. I get seam makes a lot of things easy and then I don't even think of the obvious.

            Thanks.

            Richard

            • 3. Re: Populate pojoCache from Database at startup
              pmuir

              The Seam Managed (@In) entity manager is just the extended EJB entity manager but wrapped so you don't get LazyInitialiaztionException in a conversation. Since you are caching (and so I assume the entire object graph for the entity is retrieved?) you should just be able to use a standard, non-extended EJB entity manager.

              • 4. Re: Populate pojoCache from Database at startup

                Yes. There are just data I put in the cache, so I don't need to retrieve them all the time. So what you said will work just fine.