Search in sources :

Example 91 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class EntityGraphTest method testQueryMultipleLevelsUsingLoadGraph.

public void testQueryMultipleLevelsUsingLoadGraph() {
    try {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            GraphBase base = new GraphBase(1, "First Base");
            GraphRelated related = new GraphRelated(101);
            base.setRelation(related);
            GraphRelatedNext next = new GraphRelatedNext(201);
            next.setName("First Next Relation");
            related.setRelationNext(next);
            em.persist(base);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception on persist+query", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        if (emf.getCache() != null) {
            emf.getCache().evictAll();
        }
        em = emf.createEntityManager();
        tx = em.getTransaction();
        List<GraphBase> results = null;
        try {
            tx.begin();
            EntityGraph<GraphBase> eg = em.createEntityGraph(GraphBase.class);
            eg.addAttributeNodes("name", "relation");
            Subgraph<GraphRelated> relationGraph = eg.addSubgraph("relation", GraphRelated.class);
            relationGraph.addAttributeNodes("nextRelation");
            Subgraph<GraphRelatedNext> cityGraph = relationGraph.addSubgraph("nextRelation", GraphRelatedNext.class);
            cityGraph.addAttributeNodes("name");
            Query q = em.createQuery("SELECT b FROM GraphBase b");
            q.setHint("javax.persistence.loadgraph", eg);
            results = q.getResultList();
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception on persist+query", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Will now be detached, so check detachment
        assertEquals(1, results.size());
        // Test if everything was loaded as per JPA Spec 3.2.7
        assertEquals("First Next Relation", results.get(0).getRelation().getRelationNext().getName());
    } finally {
        clean(GraphBase.class);
        clean(GraphRelated.class);
        clean(GraphRelatedNext.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) GraphBase(org.datanucleus.samples.annotations.entitygraph.GraphBase) GraphRelated(org.datanucleus.samples.annotations.entitygraph.GraphRelated) GraphRelatedNext(org.datanucleus.samples.annotations.entitygraph.GraphRelatedNext)

Example 92 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class EntityManagerFactoryTest method testPersistenceUnitUtilGetIdentifier.

/**
 * Test for emf.getPersistenceUnitUtil.getIdentifier() method
 */
public void testPersistenceUnitUtilGetIdentifier() {
    try {
        PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p.setGlobalNum("First");
            em.persist(p);
            assertTrue(util.getIdentifier(p) instanceof Person.PK);
            VersionedPerson vp = new VersionedPerson(1, 1);
            em.persist(vp);
            Object vpId = util.getIdentifier(vp);
            assertTrue(vpId instanceof Long && ((Long) vpId) == 1);
            tx.rollback();
        } catch (Exception e) {
            LOG.error(">> Exception on persist before serialisation", e);
            fail("Exception on persist : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
    // No cleanup needed
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person) PersistenceException(javax.persistence.PersistenceException)

Example 93 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class EntityManagerTest method testPersistEntityWithInterfaceField.

/**
 * Test of persistence/retrieval of object with an interface field.
 */
public void testPersistEntityWithInterfaceField() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            ShapeHolder holder = new ShapeHolder(100);
            holder.setShape1(new Rectangle(200, 45, 55));
            Circle circ = new Circle(300, 25.8);
            holder.getShapeSet1().add(circ);
            em.persist(holder);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Retrieve it
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            ShapeHolder holder = em.find(ShapeHolder.class, 100);
            assertNotNull(holder);
            Shape shape = holder.getShape1();
            assertNotNull(shape);
            assertTrue(shape instanceof Rectangle);
            Rectangle rect = (Rectangle) shape;
            assertEquals(45.0, rect.getWidth(), 0.1);
            assertEquals(55.0, rect.getLength(), 0.1);
            Set<Shape> shapes = holder.getShapeSet1();
            assertNotNull(shapes);
            assertEquals(1, shapes.size());
            Shape setShape = shapes.iterator().next();
            assertTrue(setShape instanceof Circle);
            Circle circ = (Circle) setShape;
            assertEquals(25.8, circ.getRadius(), 0.1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(ShapeHolder.class);
        clean(Rectangle.class);
        clean(Circle.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Circle(org.datanucleus.samples.types.interfaces.Circle) EntityManager(javax.persistence.EntityManager) ShapeHolder(org.datanucleus.samples.types.interfaces.ShapeHolder) Shape(org.datanucleus.samples.types.interfaces.Shape) Rectangle(org.datanucleus.samples.types.interfaces.Rectangle)

Example 94 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class EntityManagerTest method testMarkForRollbackOnError.

/**
 * Test of marking of current txn for rollback on exception.
 */
public void testMarkForRollbackOnError() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            try {
                em.find(Account.class, new Long(123));
            } catch (EntityNotFoundException enfe) {
                if (!tx.getRollbackOnly()) {
                    fail("Transaction wasn't marked for rollback after exception on object find()");
                }
            }
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }
    } finally {
        clean(Account.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException) PersistenceException(javax.persistence.PersistenceException) EntityNotFoundException(javax.persistence.EntityNotFoundException)

Example 95 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class EntityManagerTest method testPersistThenDetach.

/**
 * Test of EntityManager.persist() and detach().
 */
public void testPersistThenDetach() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p.setGlobalNum("First");
            em.persist(p);
            em.detach(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        em.close();
    } finally {
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)540 EntityManager (javax.persistence.EntityManager)447 Query (javax.persistence.Query)143 Person (org.datanucleus.samples.annotations.models.company.Person)101 List (java.util.List)90 TypedQuery (javax.persistence.TypedQuery)82 NoResultException (javax.persistence.NoResultException)77 ArrayList (java.util.ArrayList)69 NotFoundException (org.opencastproject.util.NotFoundException)61 PersistenceException (javax.persistence.PersistenceException)59 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)55 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)50 JPAQuery (org.datanucleus.api.jpa.JPAQuery)32 EntityManagerFactory (javax.persistence.EntityManagerFactory)29 Predicate (javax.persistence.criteria.Predicate)26 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)21 Connection (java.sql.Connection)20 RollbackException (javax.persistence.RollbackException)20 HashSet (java.util.HashSet)19 Employee (org.datanucleus.samples.annotations.models.company.Employee)19