Examples of EntityManager


Examples of javax.persistence.EntityManager

    /**
     * Verifies if the serRollbackOnly works.
     */
    public void setRollbackOnly() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        // begins the transaction and sets the rollbackonly
        entityManager.getTransaction().begin();
        entityManager.getTransaction().setRollbackOnly();
        // creates a book
        Book book = new Book();
        book.setId(ID);
        book.setName(ENTITY_NAME);
        entityManager.persist(book);
        // verifies if the container registered the rollback.
        assertTrue(entityManager.getTransaction().getRollbackOnly(),
                "The transaction is marked as rollback, but the container returned false for the method getRollbackOnly()");

        // tries to make the commit, the container must throw an exception.
        try {
            entityManager.getTransaction().commit();
            fail("The method setRollbackOnly was called, so the transaction cannot make the commit.");
        } catch (RollbackException e) {
            logger.info("The bean threw an expected exception");
        }
        entityManager.close();

        // verifies if the transaction was rolled back
        Book bookResult = entityManager.find(Book.class, new Integer(ID));
        assertNull(bookResult, "The container did not make the rollback");
    }
View Full Code Here

Examples of javax.persistence.EntityManager

    /**
     * Verifies if the commit works.
     */
    public void resourceLevelCommit() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        // begins the trasnaction
        entityManager.getTransaction().begin();
        // inserts a book
        Book book = new Book();
        book.setId(ID);
        book.setName(ENTITY_NAME);
        entityManager.persist(book);
        // verifies if the transaction is marked as ACTIVE
        assertTrue(entityManager.getTransaction().isActive(),
                "The transaction is active, but the container returned false when the method isActive was called.");
        // verify if the rollbackonly is not set.
        assertFalse(entityManager.getTransaction().getRollbackOnly(),
                "The transaction is not marked as rollback, but the container returned true for the method getRollbackOnly()");
        // makes a commit
        entityManager.getTransaction().commit();
        entityManager.close();
        // verifies if the bean is persistent.
        Book bookResult = entityManager.find(Book.class, new Integer(ID));
        assertNotNull(bookResult, "The container did not make the rollback");

    }
View Full Code Here

Examples of javax.persistence.EntityManager

                logger.debug("Error in EntityManagerFactory use: {0}", e);
                e.printStackTrace();
            }

        } else if (isEqual(ENTITY_MANAGER, op)) {
            EntityManager em = (EntityManager) ctx.lookup("persistence/pctx01");
            try {
                EntityManagerTester.checkInstance(em, "tmpTable" + this.hashCode());
                log(getName(), TIMEOUT, getName(), ENTITY_MANAGER);
            } catch (Exception e) {
                logger.debug("Error in EntityManagerFactory use: {0}", e);
View Full Code Here

Examples of javax.persistence.EntityManager


        } else if (ic.getMethod().toString().contains("accessEntityManagerFactory")) {

            //EntityManagerFactory Access
            EntityManager entityManager = entityFactory.createEntityManager();
            entityManager.getTransaction().begin();
            checkInstance(entityManager, EMFactoryTester.NAME);
            entityManager.getTransaction().commit();

        } else if (ic.getMethod().toString().contains("accessSessionContext")) {

            //SessionContext Access
            if (ctx == null){
View Full Code Here

Examples of javax.persistence.EntityManager

     * Creates the EntityManagerFactory and cleans the db.
     */
    public ResourceLevelTransTester() {
        // gets the EntitymanagerFactory for the persistence unite book
        entityManagerFactory = Persistence.createEntityManagerFactory("book");
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        Book bookResult = entityManager.find(Book.class, new Integer(ID));
        if (bookResult != null) {
            entityManager.remove(bookResult);
        }
    }
View Full Code Here

Examples of javax.persistence.EntityManager

    /**
     * Verifies if the rollback works.
     */
    public void resourceLevelRollback() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        // begins the transaction
        entityManager.getTransaction().begin();
        // creates a book
        Book book = new Book();
        book.setId(ID);
        book.setName(ENTITY_NAME);
        entityManager.persist(book);
        // makes a rollback
        entityManager.getTransaction().rollback();
        entityManager.close();
        // verifies if the container made the rollback
        Book bookResult = entityManager.find(Book.class, new Integer(ID));
        assertNull(bookResult, "The container did not make the rollback");
    }
View Full Code Here

Examples of javax.persistence.EntityManager

    /**
     * Verifies if the serRollbackOnly works.
     */
    public void setRollbackOnly() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        // begins the transaction and sets the rollbackonly
        entityManager.getTransaction().begin();
        entityManager.getTransaction().setRollbackOnly();
        // creates a book
        Book book = new Book();
        book.setId(ID);
        book.setName(ENTITY_NAME);
        entityManager.persist(book);
        // verifies if the container registered the rollback.
        assertTrue(entityManager.getTransaction().getRollbackOnly(),
                "The transaction is marked as rollback, but the container returned false for the method getRollbackOnly()");

        // tries to make the commit, the container must throw an exception.
        try {
            entityManager.getTransaction().commit();
            fail("The method setRollbackOnly was called, so the transaction cannot make the commit.");
        } catch (RollbackException e) {
            logger.info("The bean threw an expected exception");
        }
        entityManager.close();

        // verifies if the transaction was rolled back
        Book bookResult = entityManager.find(Book.class, new Integer(ID));
        assertNull(bookResult, "The container did not make the rollback");
    }
View Full Code Here

Examples of javax.persistence.EntityManager

    /**
     * Verifies if the commit works.
     */
    public void resourceLevelCommit() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        // begins the trasnaction
        entityManager.getTransaction().begin();
        // inserts a book
        Book book = new Book();
        book.setId(ID);
        book.setName(ENTITY_NAME);
        entityManager.persist(book);
        // verifies if the transaction is marked as ACTIVE
        assertTrue(entityManager.getTransaction().isActive(),
                "The transaction is active, but the container returned false when the method isActive was called.");
        // verify if the rollbackonly is not set.
        assertFalse(entityManager.getTransaction().getRollbackOnly(),
                "The transaction is not marked as rollback, but the container returned true for the method getRollbackOnly()");
        // makes a commit
        entityManager.getTransaction().commit();
        entityManager.close();
        // verifies if the bean is persistent.
        Book bookResult = entityManager.find(Book.class, new Integer(ID));
        assertNotNull(bookResult, "The container did not make the rollback");

    }
View Full Code Here

Examples of javax.persistence.EntityManager

     * @param ref Entity Manager Factory
     * @param name row name
     * @throws Exception if a problem occurs
     */
    public static void checkInstance(final EntityManagerFactory ref, final String name) throws Exception {
        EntityManager entityManager = ref.createEntityManager();
        logger.debug("Checking an entity manager factory reference.");

        if (entityManager == null) {
            logger.debug("The method createEntityManager() returned null.");
            throw new IllegalStateException("Error checking an entity manager factory reference.");
View Full Code Here

Examples of javax.persistence.EntityManager

      {
         ut.begin();
         try
         {
            InitialContext ctx = new InitialContext();
            EntityManager em = (EntityManager) ctx.lookup("java:comp/env/persistence/em");
           
            String mode = req.getParameter("mode");
            if ("Write".equals(mode))
            {
               TestEntity test = new TestEntity("Hello", "World");
               em.persist(test);
            }
            else
            {
               TestEntity test = em.find(TestEntity.class, "Hello");
               resp.setContentType("text/plain");
               PrintWriter out = resp.getWriter();
               out.print(test.getDescription());
               out.close();
            }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.