Search in sources :

Example 1 with GraphRelatedNext

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

use of org.datanucleus.samples.annotations.entitygraph.GraphRelatedNext 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)2 EntityTransaction (javax.persistence.EntityTransaction)2 Query (javax.persistence.Query)2 JPAQuery (org.datanucleus.api.jpa.JPAQuery)2 GraphBase (org.datanucleus.samples.annotations.entitygraph.GraphBase)2 GraphRelated (org.datanucleus.samples.annotations.entitygraph.GraphRelated)2 GraphRelatedNext (org.datanucleus.samples.annotations.entitygraph.GraphRelatedNext)2