use of javax.persistence.criteria.Join in project tests by datanucleus.
the class CriteriaMetaModelTest method testResultCase.
/**
* Test basic querying with a result including CASE expression.
*/
public void testResultCase() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Manager> crit = cb.createQuery(Manager.class);
Root<Manager> candidate = crit.from(Manager.class);
Set<Join<Manager, ?>> joins = candidate.getJoins();
// Make sure joins returns empty set
assertNotNull(joins);
assertEquals(0, joins.size());
Set<Fetch<Manager, ?>> fetches = candidate.getFetches();
// Make sure fetches returns empty set
assertNotNull(fetches);
assertEquals(0, fetches.size());
candidate.alias("m");
Path<Integer> yrsVar = candidate.get(Manager_.yearsExperience);
Predicate lessThan2 = cb.lessThan(yrsVar, 5);
crit.multiselect(candidate.get(Manager_.firstName), candidate.get(Manager_.lastName), cb.selectCase().when(lessThan2, "Junior").otherwise("Senior"));
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT m.firstName,m.lastName,CASE WHEN (m.yearsExperience < 5) THEN 'Junior' ELSE 'Senior' END FROM org.datanucleus.samples.jpa.query.Manager m", crit.toString());
Query q = em.createQuery(crit);
List<Object[]> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 2, results.size());
boolean mourinho = false;
boolean guardiola = false;
Iterator<Object[]> resultIter = results.iterator();
while (resultIter.hasNext()) {
Object[] result = resultIter.next();
assertEquals(3, result.length);
if (result[0].equals("Jose") && result[1].equals("Mourinho")) {
mourinho = true;
assertEquals("Senior", result[2]);
} else if (result[0].equals("Pep") && result[1].equals("Guardiola")) {
guardiola = true;
assertEquals("Junior", result[2]);
}
}
if (!mourinho) {
fail("Jose Mourinho not returned");
}
if (!guardiola) {
fail("Pep Guardiola not returned");
}
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.Join in project tests by datanucleus.
the class CriteriaMetaModelTest method testResultTuple.
/**
* Test basic querying with a result as a Tuple.
*/
public void testResultTuple() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Tuple> crit = cb.createTupleQuery();
Root<Manager> candidate = crit.from(Manager.class);
Set<Join<Manager, ?>> joins = candidate.getJoins();
// Make sure joins returns empty set
assertNotNull(joins);
assertEquals(0, joins.size());
Set<Fetch<Manager, ?>> fetches = candidate.getFetches();
// Make sure fetches returns empty set
assertNotNull(fetches);
assertEquals(0, fetches.size());
candidate.alias("m");
crit.multiselect(candidate.get(Manager_.firstName), candidate.get(Manager_.lastName));
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT m.firstName,m.lastName FROM org.datanucleus.samples.jpa.query.Manager m", crit.toString());
Query q = em.createQuery(crit);
List<Tuple> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 2, results.size());
boolean mourinho = false;
boolean guardiola = false;
Iterator<Tuple> resultIter = results.iterator();
while (resultIter.hasNext()) {
Tuple result = resultIter.next();
List<TupleElement<?>> tupleElements = result.getElements();
assertEquals(2, tupleElements.size());
Object elem0 = result.get(0);
Object elem1 = result.get(1);
assertTrue(elem0 instanceof String);
assertTrue(elem1 instanceof String);
String first = (String) elem0;
String last = (String) elem1;
if (first.equals("Jose") && last.equals("Mourinho")) {
mourinho = true;
} else if (first.equals("Pep") && last.equals("Guardiola")) {
guardiola = true;
}
}
if (!mourinho) {
fail("Jose Mourinho not returned");
}
if (!guardiola) {
fail("Pep Guardiola not returned");
}
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.Join in project tests by datanucleus.
the class CriteriaMetaModelTest method testResult.
/**
* Test basic querying with a result.
*/
public void testResult() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Manager> crit = cb.createQuery(Manager.class);
Root<Manager> candidate = crit.from(Manager.class);
Set<Join<Manager, ?>> joins = candidate.getJoins();
// Make sure joins returns empty set
assertNotNull(joins);
assertEquals(0, joins.size());
Set<Fetch<Manager, ?>> fetches = candidate.getFetches();
// Make sure fetches returns empty set
assertNotNull(fetches);
assertEquals(0, fetches.size());
candidate.alias("m");
crit.multiselect(candidate.get(Manager_.firstName), candidate.get(Manager_.lastName));
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT m.firstName,m.lastName FROM org.datanucleus.samples.jpa.query.Manager m", crit.toString());
Query q = em.createQuery(crit);
List<Object[]> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 2, results.size());
boolean mourinho = false;
boolean guardiola = false;
Iterator<Object[]> resultIter = results.iterator();
while (resultIter.hasNext()) {
Object[] result = resultIter.next();
assertEquals(2, result.length);
if (result[0].equals("Jose") && result[1].equals("Mourinho")) {
mourinho = true;
} else if (result[0].equals("Pep") && result[1].equals("Guardiola")) {
guardiola = true;
}
}
if (!mourinho) {
fail("Jose Mourinho not returned");
}
if (!guardiola) {
fail("Pep Guardiola not returned");
}
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.Join in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFromJoinOneToManyUsingAttributeName.
/**
* Test basic generation of query with candidate and alias, and FROM join on 1-N (using attr name).
*/
public void testBasicWithFromJoinOneToManyUsingAttributeName() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Farm> crit = cb.createQuery(Farm.class);
Root<Farm> candidate = crit.from(Farm.class);
candidate.alias("f");
crit.select(candidate);
Join animalJoin = candidate.join("f.animals");
animalJoin.alias("a");
Path nameField = animalJoin.get("name");
Predicate nameEquals = cb.equal(nameField, "Woolly Sheep");
crit.where(nameEquals);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT f FROM org.datanucleus.samples.annotations.one_many.bidir.Farm f JOIN f.animals a WHERE (a.name = 'Woolly Sheep')", crit.toString());
Query q = em.createQuery(crit);
List<Farm> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 1, results.size());
Farm farm = (Farm) results.get(0);
assertEquals("Farm is incorrect", "Kiwi Farm", farm.getName());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.Join in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFromJoinOneToOneUsingAttributeName.
/**
* Test basic generation of query with candidate and alias, and FROM join on 1-1.
*/
public void testBasicWithFromJoinOneToOneUsingAttributeName() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Employee> crit = cb.createQuery(Employee.class);
Root<Employee> candidate = crit.from(Employee.class);
candidate.alias("e");
crit.select(candidate);
Join accountJoin = candidate.join("e.account");
accountJoin.alias("a");
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT e FROM org.datanucleus.samples.annotations.models.company.Employee e JOIN e.account a", crit.toString());
Query q = em.createQuery(crit);
List<Employee> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 2, results.size());
Iterator<Employee> iter = results.iterator();
boolean joeExists = false;
boolean nigelExists = false;
while (iter.hasNext()) {
Employee emp = iter.next();
if (emp.getFirstName().equals("Nigel") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 106) {
nigelExists = true;
} else if (emp.getFirstName().equals("Joe") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 105) {
joeExists = true;
}
}
assertTrue("Nigel not present", nigelExists);
assertTrue("Joe not present", joeExists);
tx.rollback();
} catch (Exception e) {
LOG.error("Exception thrown during test", e);
fail("Exception caught during test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations