Search in sources :

Example 86 with EntityTransaction

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

the class DatastoreIdentityTest method testBasic.

/**
 * Basic test.
 */
public void testBasic() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        Object id = null;
        try {
            tx.begin();
            DSIDHolder holder = new DSIDHolder("First Holder");
            em.persist(holder);
            em.flush();
            id = NucleusJPAHelper.getObjectId(holder);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        emf.getCache().evictAll();
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Object key = IdentityUtils.getTargetKeyForDatastoreIdentity(id);
            DSIDHolder holder = em.find(DSIDHolder.class, key);
            assertNotNull(holder);
            assertEquals("First Holder", holder.getName());
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(DSIDHolder.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) DSIDHolder(org.datanucleus.samples.annotations.datastoreidentity.DSIDHolder)

Example 87 with EntityTransaction

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

the class EmbeddedTest method testOneToManyEmbeddedElements.

/**
 * Test of 1-N relation with embedded elements.
 */
public void testOneToManyEmbeddedElements() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION)) {
        return;
    }
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Processor proc = new Processor(1, "RISC");
            Job job1 = new Job("Cron backup", 1);
            proc.addJob(job1);
            Job job2 = new Job("Jenkins", 2);
            proc.addJob(job2);
            em.persist(proc);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown creating data", e);
            fail("Exception thrown while creating data (see log for details) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Processor proc = em.find(Processor.class, 1);
            assertNotNull(proc);
            List<Job> jobs = proc.getJobs();
            assertNotNull(jobs);
            assertEquals(2, jobs.size());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception thrown retrieving data", e);
            fail("Exception thrown while retrieving data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Processor.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Processor(org.datanucleus.samples.annotations.embedded.Processor) Job(org.datanucleus.samples.annotations.embedded.Job)

Example 88 with EntityTransaction

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

the class EmbeddedTest method testOneToManyWithEmbeddedId.

/**
 * Test of 1-N relation when the owner has an embedded id.
 */
public void testOneToManyWithEmbeddedId() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Department dept = new Department("Marketing");
            DepartmentPK deptPK = new DepartmentPK(101, "Mkt");
            dept.setPrimaryKey(deptPK);
            Project prj1 = new Project("DN 2.0", 100000);
            dept.getProjects().add(prj1);
            em.persist(dept);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown creating data", e);
            fail("Exception thrown while creating data (see log for details) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Query q = em.createQuery("SELECT d FROM " + Department.class.getName() + " d");
            List<Department> depts = q.getResultList();
            assertNotNull("Returned Department List is null", depts);
            assertEquals("Number of Departments is incorrect", 1, depts.size());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception thrown retrieving data", e);
            fail("Exception thrown while retrieving data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Department.class);
        clean(Project.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Project(org.datanucleus.samples.annotations.models.company.Project) EntityManager(javax.persistence.EntityManager) Department(org.datanucleus.samples.annotations.models.company.Department) Query(javax.persistence.Query) DepartmentPK(org.datanucleus.samples.annotations.models.company.DepartmentPK)

Example 89 with EntityTransaction

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

the class EntityGraphTest method testQueryNamedEntityGraph_LoadGraph.

public void testQueryNamedEntityGraph_LoadGraph() {
    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);
            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();
        try {
            tx.begin();
            EntityGraph eg = em.getEntityGraph("baseGraph");
            Query q = em.createQuery("SELECT b FROM GraphBase b");
            q.setHint("javax.persistence.loadgraph", eg);
            List<GraphBase> results = q.getResultList();
            // Check internal implementation setting groups to just this group
            Set<String> fpgroups = ((JPAQuery) q).getFetchPlan().getGroups();
            assertEquals(2, fpgroups.size());
            assertTrue(fpgroups.contains("default"));
            assertTrue(fpgroups.contains("baseGraph"));
            GraphBase result = results.get(0);
            PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();
            assertTrue(util.isLoaded(result, "id"));
            assertTrue(util.isLoaded(result, "name"));
            assertTrue(util.isLoaded(result, "relation"));
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception on persist+query", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(GraphBase.class);
        clean(GraphRelated.class);
    }
}
Also used : EntityGraph(javax.persistence.EntityGraph) JPAEntityGraph(org.datanucleus.api.jpa.JPAEntityGraph) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) GraphBase(org.datanucleus.samples.annotations.entitygraph.GraphBase) GraphRelated(org.datanucleus.samples.annotations.entitygraph.GraphRelated)

Example 90 with EntityTransaction

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

the class EntityGraphTest method testQueryDefinedEntityGraph_FetchGraph.

/**
 * Test of specification and registering of a defined EntityGraph.
 */
public void testQueryDefinedEntityGraph_FetchGraph() {
    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);
            em.persist(base);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception on persist", 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();
        try {
            tx.begin();
            EntityGraph<GraphBase> eg = em.createEntityGraph(GraphBase.class);
            eg.addAttributeNodes("id");
            eg.addAttributeNodes("name");
            eg.addAttributeNodes("relation");
            assertNull(eg.getName());
            Query q = em.createQuery("SELECT b FROM GraphBase b");
            q.setHint("javax.persistence.fetchgraph", eg);
            List<GraphBase> results = q.getResultList();
            LOG.info(">> FetchPlan=" + ((JPAQuery) q).getFetchPlan());
            Set<String> fpgroups = ((JPAQuery) q).getFetchPlan().getGroups();
            assertEquals(1, fpgroups.size());
            LOG.info(">> FPgroups=" + StringUtils.collectionToString(fpgroups));
            GraphBase result = results.get(0);
            PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();
            assertTrue(util.isLoaded(result, "id"));
            assertTrue(util.isLoaded(result, "name"));
            assertTrue(util.isLoaded(result, "relation"));
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception on persist+query", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(GraphBase.class);
        clean(GraphRelated.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) GraphBase(org.datanucleus.samples.annotations.entitygraph.GraphBase) JPAQuery(org.datanucleus.api.jpa.JPAQuery) GraphRelated(org.datanucleus.samples.annotations.entitygraph.GraphRelated)

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