Hi Jon, Not sure you can lookup by unit name. Here is a related post with David's answer.
http://www.nabble.com/Obtaining-EntityManager-programatically-for-test-purposes-td21810387.html http://www.nabble.com/Obtaining-EntityManager-programatically-for-test-purposes-td21810387.html Jean-Louis Jonathan Gallimore-2 wrote: > > Hi Chan, > > In the latest release I believe you can lookup the EntityManager using the > java:openejb context. For example for the movie-unit persistence unit in > the > injection-of-entitymanager sample you could use: > > context.lookup("java:openejb/PersistenceUnit/movie-unit"); > > to lookup the EntityManagerFactory. I tend to use some code like this to > dig > around the java:openejb context to see what goodies are there: > > NamingEnumeration list = ctx.listBindings("java:openejb"); > while (list.hasMore()) { > Binding item = (Binding) list.next(); > String className = item.getClassName(); > String name = item.getName(); > System.out.println(className + " " + name); > } > > I tried this using an embedded instance of OpenEJB, but I would imagine it > would work in Tomcat as well. > > Hope that helps, > > Jon > > > > On Tue, Jul 28, 2009 at 9:44 PM, Chan Kelwin Hillary > <[email protected]>wrote: > >> Hi, >> I have a stateless EJB Session bean that successfully instantiates the >> EntityManger using the @PersistenceContext annotation. However, due to an >> issue, I need to get the EntityManager by lookup instead. I've tried the >> code below in the stateless session bean but it is giving me the error >> "Name >> Persistence is not bound in this Context". I've also placed >> the eclipselink.jar file into the %TOMCAT%\lib directory. >> >> try { >> Context context = new InitialContext(); >> em = (javax.persistence.EntityManager) context.lookup("Persistence"); >> } catch (NamingException e) { >> System.out.println(e.getExplanation()); >> } >> >> persistence.xml >> =================== >> <?xml version="1.0" encoding="windows-1252" ?> >> <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >> xsi:schemaLocation="http://java.sun.com/xml/ns/persistence >> http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" >> version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"> >> <persistence-unit name="Persistence"> >> <provider> >> org.eclipse.persistence.jpa.PersistenceProvider >> </provider> >> <jta-data-source> >> java:openejb/Connector/Default JDBC Database >> </jta-data-source> >> <class> >> com.testejb.model.ejb.dn.Dept >> </class> >> <properties> >> <property name="eclipselink.target-database" value="Oracle10g"/> >> </properties> >> </persistence-unit> >> </persistence> >> >> I am running this on Tomcat-6.0.20 (extracted from the zip file instead >> of >> the executable) and the embedded OpenEJB. Can this be done? What am I >> missing? >> >> Thanks. >> >> >> -- >> Best regards, >> >> Chan Kelwin >> > > -- View this message in context: http://www.nabble.com/Lookup-EntityManager-from-Stateless-EJB-Session-Bean-tp24708299p24733777.html Sent from the OpenEJB User mailing list archive at Nabble.com.
