Seam-TestNG-JTA EntityManager cannot access a transactions
aimee Feb 26, 2008 10:17 PMEclipse 3.3.3.1, Eclipse TestNG plugin installed, JBoss Tools plugin installed; JBoss 4.2.2; Seam 2.0.0
Using Eclipse, created a Seam Web project. Again used Eclipse to create a Seam Entity call Product (copied code from dvdstore example). Created a simple test class and copied/modified code from dvdstore ProductUnitTest class:
public class ProductUnitTest extends SeamTest { private EntityManagerFactory emf = null; private EntityManager em = null; @Test public void testCreateDelete() { try { System.out.println("creating EntityManagerFactory and EntityManager"); EntityManagerFactory emf = Persistence.createEntityManagerFactory("Simple"); EntityManager em = emf.createEntityManager(); assertNotNull("entity manager", em); assertTrue("entity manager open", em.isOpen()); Product p = new Product(); p.setTitle("test"); System.out.println("persisting Product..."); em.getTransaction().begin(); em.persist(p); em.getTransaction().commit(); System.out.println("verifying product id > 0..."); long id = p.getProductId(); assertTrue("product id set", id != 0); System.out.println("fetching Product..."); p = em.find(Product.class, id); assertNotNull("find by id", p); assertEquals("id", id, p.getProductId()); assertEquals("title", "test", p.getTitle()); System.out.println("removing Product..."); em.getTransaction().begin(); em.remove(p); em.getTransaction().commit(); System.out.println("verifying no Product in database..."); p = em.find(Product.class, id); assertNull("deleted product", p); } catch (PersistenceException e) { e.printStackTrace(); fail(); } finally { try { if (em != null) em.close(); if (emf != null) emf.close(); }catch(Exception e){ System.out.println("exception in finally block"); e.printStackTrace(); } } } }//end of class
Then EntityManager gets created but the code blows on 'em.getTransaction().begin();'
[Invoker 32079775] Invoking @BeforeMethod org.jboss.seam.mock.SeamTest.begin() [Invoker 32079775] Invoking org.domain.Simple.test.ProductUnitTest.testCreateDelete creating EntityManagerFactory and EntityManager persisting Product... [Invoker 32079775] Invoking @AfterMethod org.jboss.seam.mock.SeamTest.end() [Invoker 32079775] Invoking @AfterClass org.jboss.seam.mock.SeamTest.cleanup() *********** INVOKED METHODS org.jboss.seam.mock.SeamTest.init() 27653945 org.domain.Simple.test.ProductUnitTest.testCreateDelete() 27653945 org.jboss.seam.mock.SeamTest.cleanup() 27653945 *********** Creating C:\SVN\Projects\Simple-test\test-output\Simple-test\org.domain.Simple.test.ProductUnitTest.html FAILED: testCreateDelete java.lang.IllegalStateException: JTA EntityManager cannot access a transactions at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:316) at org.domain.Simple.test.ProductUnitTest.testCreateDelete(ProductUnitTest.java:67) ... Removed 22 stack frames =============================================== org.domain.Simple.test.ProductUnitTest Tests run: 1, Failures: 1, Skips: 0 ===============================================
Any idea what's going on? What am I missing?
My colleague and I have a week left to put together a demo/proof of concept based on an existing ASP.NET complex workflow web app. The demo has to show off the rules engine, BPM engine, Seam conversations etc in hopes of porting the stupid asp.net app to Seam. Despite studying numerous examples and reading copious amounts of documentation, we're encountering more trouble than success. Any help is greatly appreciated!!!
aimee