Search in sources :

Example 1 with GraphBase

use of org.datanucleus.samples.annotations.entitygraph.GraphBase 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 2 with GraphBase

use of org.datanucleus.samples.annotations.entitygraph.GraphBase 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)

Example 3 with GraphBase

use of org.datanucleus.samples.annotations.entitygraph.GraphBase 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 4 with GraphBase

use of org.datanucleus.samples.annotations.entitygraph.GraphBase in project tests by datanucleus.

the class EntityGraphTest method testQueryNamedEntityGraph_FetchGraph.

public void testQueryNamedEntityGraph_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+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.fetchgraph", eg);
            List<GraphBase> results = q.getResultList();
            // Check internal implementation setting groups to just this group
            LOG.info(">> Query : FetchPlan=" + ((JPAQuery) q).getFetchPlan());
            Set<String> fpgroups = ((JPAQuery) q).getFetchPlan().getGroups();
            assertEquals(1, fpgroups.size());
            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) JPAQuery(org.datanucleus.api.jpa.JPAQuery) GraphRelated(org.datanucleus.samples.annotations.entitygraph.GraphRelated)

Example 5 with GraphBase

use of org.datanucleus.samples.annotations.entitygraph.GraphBase in project tests by datanucleus.

the class EntityGraphTest method testQueryMultipleLevelsUsingFetchJoin.

/**
 * This is the equivalent of testQueryMultipleLevelsUsingLoadGraph but using FETCH JOIN.
 */
public void testQueryMultipleLevelsUsingFetchJoin() {
    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();
            Query q = em.createQuery("SELECT b FROM GraphBase b JOIN FETCH b.relation r JOIN FETCH r.nextRelation n");
            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)

Aggregations

EntityManager (javax.persistence.EntityManager)5 EntityTransaction (javax.persistence.EntityTransaction)5 Query (javax.persistence.Query)5 JPAQuery (org.datanucleus.api.jpa.JPAQuery)5 GraphBase (org.datanucleus.samples.annotations.entitygraph.GraphBase)5 GraphRelated (org.datanucleus.samples.annotations.entitygraph.GraphRelated)5 PersistenceUnitUtil (javax.persistence.PersistenceUnitUtil)3 EntityGraph (javax.persistence.EntityGraph)2 JPAEntityGraph (org.datanucleus.api.jpa.JPAEntityGraph)2 GraphRelatedNext (org.datanucleus.samples.annotations.entitygraph.GraphRelatedNext)2