users@glassfish.java.net

Re: Glassfish, Spring, EJB - EntityManagers

From: <glassfish_at_javadesktop.org>
Date: Wed, 08 Apr 2009 14:58:34 PDT

No bites yet...

Well, here is some more thoughts/questions that may help clarify what I'm trying to do:

[i]@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class MySpringEnabledEJB implements MySpringEnabledEJBRemote {

    @PersistenceContext(name="persistence/my_pu")
    private EntityManager em;

    @Autowired
    private SomeSpringBean ssb;

    public void doSomething() {
       this.ssb.doSomething();
    }
}

---
public class SomeSpringBean {
   private EntityManager em;
   public void setEntityManager(EntityManager em) {
      this.em = em;
   }
   public void doSomething() {
      SomeEntity se = this.em.find(SomeEntity.class, 1);
      System.out.println(se.getName());
   }
}
---
@Entity
public class SomeEntity {
   @Id
    private Long id;
    private String name;
    public String getName() { return this.name; }
}
---
beanRefContext.xml
    <beans>
        <bean name="my-context"
            class="org.springframework.context.support.ClassPathXmlApplicationContext">
            <constructor-arg value="springBeans.xml"/>
        </bean>
    </beans>
---
springBeans.xml
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/jee
          http://www.springframework.org/schema/jee/spring-jee.xsd">
      
        <bean id="ssb" class="springejb.SomeSpringBean">
            <property name="entityManager" ref="entityManager"/>
        </bean>
    
        <jee:jndi-lookup id="entityManager"
            jndi-name="java:comp/env/persistence/my_pu" resource-ref="false"  />
    </beans>[/i]
---
That all works.   But the thing that sort of frustrates me is that I have to do @PersistenceContext(name="persistence/my_pu") in an EJB even though that EJB doesn't need an EntityManager - I'm just doing that to get an EntityManager into JNDI component naming context so that Spring is happy later when the Interceptor handles injecting the @Autowired beans.
So, I tried doing this:
[i]
public abstract class MyAbstractEJB {
    @PersistenceContext(name = "persistence/my_pu")
    protected EntityManager em;
}
[/i]
and then changed MySpringEnabledEJB definition to 
[i]@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class MySpringEnabledEJB extends MyAbstractEJB implements MySpringEnabledEJBRemote { ... }
[/i]
The idea is that I can put "helper" code into MyAbstractEJB (basically just to hide it and to help insure that "persistence/my_pu" is always available) and then all of my EJBs would just extend it and any spring enabled injection in that session would be able to get an EntityManager from JNDI if it needed to.
Is there a better way to do this?
--- background ---
I'm trying to put business logic into Spring enabled POJOs.  One major driving force is that I want stuff to be more easily testable and configurable.  I'm mainly using EJB as a facade to the POJO logic.  Utilizing EJB for transactional management, remote access and clustering seems ideal.  Using Spring for wiring together business logic and testing seems ideal.   I'm trying to combine the two.   Is anybody else doing something similar or wanting to do something similar?  Has anybody tried this and run away crying? ;-)
Thanks
[Message sent by forum member 'euqaz' (euqaz)]
http://forums.java.net/jive/thread.jspa?messageID=341242