5 Replies Latest reply on Aug 25, 2006 11:10 PM by Ales Justin

    Spring and EJB3 EntityManager

    Jacob Boje Newbie

      HI

      In a normal stateless session bean, the entitymanager is injected like this:

       @PersistenceContext(unitName = "MM")
       private EntityManager em;
      


      Is it possible to wire up the entitymanager with SPRING. Ex. I would like to get some DAO's injected into my stateless session bean, which automatically has been initialized with an entitymanager by the spring framework.

      Ex. how to wire up this bean in spring:

      public class MyDao {
       private EntityManager em;
      
       public void setEntityManager(EntityManager em) {
       this.em = em;
       }
      }
      


      When it is wired up it should be possible to inject it into a stateless bean:

      @Stateless
      public class myBean implements myBeanLocal {
       @Spring(jndiName = "mm", bean="myDao")
       private MyDao myDao;
      
       public void createSome() {
       myDao.save(someObject);
       }
      }
      
      Does anyone have an example for this trick on JBOSS 4.0.4GA
      
      I have no problems wiring up POJO's but it seems to be more difficult to define the entitymanager in the spring xml file.
      


        • 1. Re: Spring and EJB3 EntityManager
          Ales Justin Master

          This is what first comes to my mind:

          @Stateless
          public class myBean implements myBeanLocal {
          
           @PersistenceContext(unitName = "MM")
           private EntityManager em;
          
           @Spring(jndiName = "mm", bean="myDao")
           private MyDao myDao;
          
           @AroundInvoke
           public Object injectEM2DAO(InvocationContext c) {
           myDAO.setEM(em);
           return c.proceed();
           }
          
           public void createSome() {
           myDao.save(someObject);
           }
          
          }
          


          • 2. Re: Spring and EJB3 EntityManager
            Jacob Boje Newbie

            Thank you. This is one way of doing it. But I hope to find a solution where the EntityManager setting in the dao is handled totally in spring. Then it only would be neccesary to inject the dao into the session beans.

            I'm thinking on a spring context like this:

             <bean id="myDao" class="somePackage.myDao">
             <property name="entityManager" ref="entityManager"/>
             </bean>
             <bean id="entityManagerFactory" class="someEntityManagerClass">
             .....
             </bean>
            


            Has anyone tried to wire up the EntityManager in spring?

            • 3. Re: Spring and EJB3 EntityManager
              Mark Menard Newbie

              So, has anyone gotten this working?

              I tried adding:




              type entries to my persistance.xml file, but the EntityManager and the factory are not being exposed via jndi.

              Thanks,

              Mark

              • 4. Re: Spring and EJB3 EntityManager
                Mark Menard Newbie

                My appologies for the double post. I forgot the code tags.

                So, has anyone gotten this working?

                I tried adding:

                <property name="jboss.entity.manager.jndi.name" value="java:/Manager1"/>
                <property name="jboss.entity.manager.factory.jndi.name" value="java:/Manager1Factory"/>
                


                type entries to my persistance.xml file, but the EntityManager and the factory are not being exposed via jndi.

                Thanks,

                Mark

                • 5. Re: Spring and EJB3 EntityManager
                  Ales Justin Master

                  What's the real problem here? Ok, all Spring stuff is by default exposed by jboss later than ejb3, but that is plain config stuff.