use of org.datanucleus.api.jpa.JPAQuery 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);
}
}
use of org.datanucleus.api.jpa.JPAQuery 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);
}
}
use of org.datanucleus.api.jpa.JPAQuery in project tests by datanucleus.
the class JPQLQueryTest method testSaveAsNamedQuery.
/**
* Test for saving a query as a named query via the Query (DN extension).
*/
public void testSaveAsNamedQuery() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@gmail.com");
em.persist(p1);
Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@gmail.com");
em.persist(p2);
em.flush();
Query q1 = em.createQuery("SELECT p FROM Person_Ann p WHERE p.lastName = :param");
q1.setParameter("param", "Flintstone");
List<Person> results1 = q1.getResultList();
assertEquals(1, results1.size());
assertEquals("Flintstone", ((Person) results1.iterator().next()).getLastName());
// Save this query for later use
((JPAQuery) q1).saveAsNamedQuery("PeopleWithLastNameFromQuery");
Query q2 = em.createNamedQuery("PeopleWithLastNameFromQuery");
assertEquals("SELECT p FROM Person_Ann p WHERE p.lastName = :param", q2.toString());
tx.rollback();
} catch (PersistenceException e) {
LOG.error("Exception in test", e);
fail("Exception in Named Query test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Person.class);
}
}
use of org.datanucleus.api.jpa.JPAQuery in project tests by datanucleus.
the class JPQLSingleStringParserTest method testSubqueryAnd.
/**
* Test for the parse of a subquery and conversion into the correct components.
*/
public void testSubqueryAnd() {
EntityManager em = emf.createEntityManager();
String str = "SELECT T FROM org.jpox.samples.MyClass T WHERE " + "T.field1 < (SELECT AVG(S.price) FROM org.jpox.samples.MyClass S)" + " AND T.field2 == 'Some String'";
Query q = em.createQuery(str);
AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
try {
parser.parse();
assertEquals("Filter is incorrect", "T.field1 < DN_SUBQUERY_1 AND T.field2 == 'Some String'", query.getFilter());
SubqueryDefinition subqDef = query.getSubqueryForVariable("DN_SUBQUERY_1");
assertNotNull("No subquery defined", subqDef);
org.datanucleus.store.query.Query subq = subqDef.getQuery();
assertEquals("Subquery result is incorrect", "AVG(S.price)", subq.getResult());
assertNull("Subquery filter is not null", subq.getFilter());
} catch (NucleusUserException e) {
fail("Exception thrown in parse " + e.getMessage());
}
}
use of org.datanucleus.api.jpa.JPAQuery in project tests by datanucleus.
the class JPQLSingleStringParserTest method testInsert.
/**
* Test for the parse of an INSERT query and conversion into the correct components.
*/
public void testInsert() {
EntityManager em = emf.createEntityManager();
String str = "INSERT INTO org.jpox.samples.MyClass (id, name, otherField) SELECT p.id, p.name, p.description FROM org.jpox.samples.Person p WHERE p.id > 3";
Query q = em.createQuery(str);
AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
try {
parser.parse();
assertEquals("Type is wrong", QueryType.BULK_INSERT, query.getType());
assertEquals("Candidate is wrong", "org.jpox.samples.MyClass", query.getFrom());
assertEquals("INSERT fields are wrong", "id, name, otherField", query.getInsertFields());
assertEquals("INSERT select query is wrong", "SELECT p.id, p.name, p.description FROM org.jpox.samples.Person p WHERE p.id > 3", query.getInsertSelectQuery());
} catch (NucleusUserException nue) {
fail("Exception thrown in parse " + nue.getMessage());
}
}
Aggregations