Search in sources :

Example 11 with JPAQuery

use of org.datanucleus.api.jpa.JPAQuery 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)

Aggregations

EntityManager (javax.persistence.EntityManager)11 Query (javax.persistence.Query)11 JPAQuery (org.datanucleus.api.jpa.JPAQuery)11 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)7 JPQLSingleStringParser (org.datanucleus.query.JPQLSingleStringParser)7 AbstractJPQLQuery (org.datanucleus.store.query.AbstractJPQLQuery)7 EntityTransaction (javax.persistence.EntityTransaction)4 SubqueryDefinition (org.datanucleus.store.query.Query.SubqueryDefinition)4 PersistenceUnitUtil (javax.persistence.PersistenceUnitUtil)3 GraphBase (org.datanucleus.samples.annotations.entitygraph.GraphBase)3 GraphRelated (org.datanucleus.samples.annotations.entitygraph.GraphRelated)3 EntityGraph (javax.persistence.EntityGraph)2 JPAEntityGraph (org.datanucleus.api.jpa.JPAEntityGraph)2 PersistenceException (javax.persistence.PersistenceException)1 TypedQuery (javax.persistence.TypedQuery)1 Person (org.datanucleus.samples.annotations.models.company.Person)1