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);
}
}
Aggregations