This content has been marked as final.
Show 2 replies
-
1. Re: Lookup EntityManager without declaring @PersistenceConte
sisepago Mar 23, 2006 7:38 AM (in response to eekboom)Hi,
I get the same problem when I try to run the testCase EmbeddedEjb3TestCase.java comes with the Embedded-EJB3-ALPHA_5 examples.public class EmbeddedEjb3TestCase extends TestCase { public EmbeddedEjb3TestCase() { super("EmbeddedEjb3TestCase"); } public static Test suite() throws Exception { TestSuite suite = new TestSuite(); suite.addTestSuite(EmbeddedEjb3TestCase.class); // setup test so that embedded JBoss is started/stopped once for all tests here. TestSetup wrapper = new TestSetup(suite) { protected void setUp() { startupEmbeddedJboss(); } protected void tearDown() { shutdownEmbeddedJboss(); } }; return wrapper; } public static void startupEmbeddedJboss() { EJB3StandaloneBootstrap.boot(null); EJB3StandaloneBootstrap.scanClasspath(); } public static void shutdownEmbeddedJboss() { EJB3StandaloneBootstrap.shutdown(); } public void testEntityManager() throws Exception { // This is a transactionally aware EntityManager and must be accessed within a JTA transaction // Why aren't we using javax.persistence.Persistence? Well, our persistence.xml file uses // jta-datasource which means that it is created by the EJB container/embedded JBoss. // using javax.persistence.Persistence will just cause us an error EntityManager em = (EntityManager) getInitialContext().lookup("java:/custdb"); // Obtain JBoss transaction UserTransaction tm = (UserTransaction) getInitialContext().lookup("UserTransaction"); tm.begin(); Customer cust = new Customer(); cust.setName("Bill"); em.persist(cust); assertTrue(cust.getId() > 0); int id = cust.getId(); System.out.println("created bill in DB with id: " + id); tm.commit(); tm.begin(); cust = em.find(Customer.class, id); assertNotNull(cust); tm.commit(); } public static InitialContext getInitialContext() throws Exception { Hashtable props = getInitialContextProperties(); return new InitialContext(props); } private static Hashtable getInitialContextProperties() { Hashtable props = new Hashtable(); props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory"); props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); return props; } }
Tell me do you have a solution for this problem or how I have to inject EntityManages in the embedded-jboss-*.xml file ?
Thanks ... -
2. Re: Lookup EntityManager without declaring @PersistenceConte
sisepago Mar 23, 2006 11:23 AM (in response to eekboom)Try this:
your persistence.xml file must look like this<persistence> <persistence-unit name="foodb"> <jta-data-source>java/DefaultDS</jta-data-source> <properties> <property name="jboss.entity.manager.jndi.name" value="java:/EntityManager/foodb"/> </properties> </persistence-unit> </persistence>
and then in your code just look up EntityManager like thisEntityManager em = (EntityManager)getJBossSpecificInitialContext().lookup("java:/EntityManager/foodb");
I hope this can help u to roll your effort.