Glassfish, Spring, EJB - EntityManagers

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Glassfish, Spring, EJB - EntityManagers

glassfish-2
Hello,

I'm working on an application and trying to find a good way to integrate Spring into EJB3.  My goal is to be able to inject an EntityManager into my Spring beans so that they can access entities and the database when needed.  When running in Glassfish I would like my Spring beans to get a container managed EntityManager from Glassfish and when running in a testing capacity I'd like to be able to use an entity manager provided by Spring.

Glassfish doesn't seem publish the Persistence Unit to JNDI (unless I do something like this in my EJB which makes it available for the session:

[i]@PersistenceContext(name = "persistence/em")
private EntityManager em;[/i]

So it doesn't seem like I can just simply lookup the Glassfish managed EntityManager from JNDI in my Spring configuration - or am I wrong?

Ultimately I'd like to be able to use Spring's JpaTemplate inside my Spring beans but I'd still like to get the EntityManager from Glassfish if possible.

Any ideas?  Anybody using Glassfish with Spring, JPA and EJB3?
[Message sent by forum member 'euqaz' (euqaz)]

http://forums.java.net/jive/thread.jspa?messageID=341066

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Glassfish, Spring, EJB - EntityManagers

glassfish-2
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

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Glassfish, Spring, EJB - EntityManagers

glassfish-2
Try inserting this in your springBeans.xml:

<context:annotation-config />
<tx:annotation-driven />

and then, do you use any EntityManagerFactory?

c ya
[Message sent by forum member 'lfugaro' (lfugaro)]

http://forums.java.net/jive/thread.jspa?messageID=341382

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

fashion tiffanyforsale

glassfish-2
In reply to this post by glassfish-2
<li><h1><a href="http://www.tiffanyforsale.us">fashion tiffanyforsale</a></h1></li>
<li><h1><a href="http://uggforsale.us/">ugg shoes for cheap</a></h1></li>
<li><h1><a href="http://easyforbuy.com/big-size-shoes-c-246.html?">big size shoes</a></h1></li>
<li><h1><a href="http://easyforbuy.com/women-shoes-c-7.html">women shoes for cheap</a></h1></li>
<li><h1><a href="http://easyforbuy.com/women-shoes-women-prada-shoes-c-7_244.html">prada shoes for women</a></h1></li>
<li><h1><a href="http://easyforbuy.com/kid-shoes-c-8.html"> puma childrens shoes</a></h1></li>
<li><h1><a href="http://lowestmall.com/women-shoes-women-nike-air-force-one-c-7_83.html">women nike shoes</a></h1></li>
<li><h1><a href="http://lowestmall.com/big-size-shoes-c-240.html">shoes for women with big feet</a></h1></li>
<li><h1><a href="http://lowestmall.com/puma-shoes-c-18.html">discount puma shoes</a></h1></li>
<li><h1><a href="http://sellbestshoes.com/">puma shoes sale</a></h1></li>
<li><h1><a href="http://sellbestshoes.com/">timberland boots on sale</a></h1></li>
<li><h1><a href="http://watchinstyle.com/rado-c-39.html?">rado watches discounted</a></h1></li>
<li><h1><a href="http://watchinstyle.com/rolex-datejusts-c-5.html">cheap rolex watches</a></h1></li>
<li><h1><a href="http://watchinstyle.com/rolex-sports-models-c-4.html">rolex watches for sale</a></h1></li>
<li><h1><a href="http://watchinstyle.com/rolex-datejusts-oyster-perpetual-ss-c-5_85.html">buy rolex watches online</a></h1></li>
[Message sent by forum member 'denise2013' (denise2013)]

http://forums.java.net/jive/thread.jspa?messageID=341559

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Glassfish, Spring, EJB - EntityManagers

ghorbelf
This post has NOT been accepted by the mailing list yet.
In reply to this post by glassfish-2
Hi.

Could you tell what you did to get the entitymanger from jndi

Me i put that in my code and i can not get an instance of entitymanager
 <jee:jndi-lookup id="entityManager"  jndi-name="java:comp/env/persistence/dbaasPU" resource-ref="false"/>

the error

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
Loading...