use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithoutAlias.
/**
* Test basic generation of query with candidate and without the alias.
*/
public void testBasicWithoutAlias() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Person> crit = cb.createQuery(Person.class);
Root<Person> candidate = crit.from(Person.class);
crit.select(candidate);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT DN_THIS FROM org.datanucleus.samples.annotations.models.company.Person DN_THIS", crit.toString());
Query q = em.createQuery(crit);
List<Person> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 4, results.size());
Iterator<Person> iter = results.iterator();
boolean nigelBloggsPresent = false;
boolean joeBloggsPresent = false;
boolean fredFlintstonePresent = false;
boolean barneyRubblePresent = false;
while (iter.hasNext()) {
Person pers = iter.next();
if (pers.getFirstName().equals("Fred") && pers.getLastName().equals("Flintstone") && pers.getPersonNum() == 101) {
fredFlintstonePresent = true;
}
if (pers.getFirstName().equals("Barney") && pers.getLastName().equals("Rubble") && pers.getPersonNum() == 102) {
barneyRubblePresent = true;
}
if (pers.getFirstName().equals("Joe") && pers.getLastName().equals("Bloggs") && pers.getPersonNum() == 105) {
joeBloggsPresent = true;
}
if (pers.getFirstName().equals("Nigel") && pers.getLastName().equals("Bloggs") && pers.getPersonNum() == 106) {
nigelBloggsPresent = true;
}
}
assertTrue("Fred Flintstone was not returned!", fredFlintstonePresent);
assertTrue("Barney Rubble was not returned!", barneyRubblePresent);
assertTrue("Joe Bloggs was not returned!", joeBloggsPresent);
assertTrue("Fred Bloggs was not returned!", nigelBloggsPresent);
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithOrder.
/**
* Test basic generation of query with candidate and alias, with ordering.
*/
public void testBasicWithOrder() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Person> crit = cb.createQuery(Person.class);
Root<Person> candidate = crit.from(Person.class);
candidate.alias("p");
crit.select(candidate);
Path firstNameField = candidate.get("firstName");
Order orderFirstName = cb.desc(firstNameField);
crit.orderBy(orderFirstName);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p ORDER BY p.firstName DESC", crit.toString());
Query q = em.createQuery(crit);
List<Person> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 4, results.size());
Person per0 = results.get(0);
Person per1 = results.get(1);
Person per2 = results.get(2);
Person per3 = results.get(3);
if (!per0.getFirstName().equals("Nigel") || !per0.getLastName().equals("Bloggs") || per0.getPersonNum() != 106) {
fail("Nigel Bloggs was not result 0");
}
if (!per1.getFirstName().equals("Joe") || !per1.getLastName().equals("Bloggs") || per1.getPersonNum() != 105) {
fail("Joe Bloggs was not result 1");
}
if (!per2.getFirstName().equals("Fred") || !per2.getLastName().equals("Flintstone") || per2.getPersonNum() != 101) {
fail("Fred Flintstone was not result 2");
}
if (!per3.getFirstName().equals("Barney") || !per3.getLastName().equals("Rubble") || per3.getPersonNum() != 102) {
fail("Barney Rubble was not result 3");
}
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFilterStringUPPERLOWER.
/**
* Test basic generation of query with candidate and alias and filter (UPPER/LOWER).
*/
public void testBasicWithFilterStringUPPERLOWER() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Person> crit = cb.createQuery(Person.class);
Root<Person> candidate = crit.from(Person.class);
candidate.alias("p");
crit.select(candidate);
Path firstNameField = candidate.get("firstName");
Expression firstNameUpperExpr = cb.upper(firstNameField);
Predicate firstNameUpperEquals = cb.equal(firstNameUpperExpr, "FRED");
Path lastNameField = candidate.get("lastName");
Expression lastNameLowerExpr = cb.lower(lastNameField);
Predicate lastNameUpperEquals = cb.equal(lastNameLowerExpr, "flintstone");
crit.where(firstNameUpperEquals, lastNameUpperEquals);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE" + " (UPPER(p.firstName) = 'FRED') AND (LOWER(p.lastName) = 'flintstone')", crit.toString());
Query q = em.createQuery(crit);
List<Person> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 1, results.size());
Iterator<Person> iter = results.iterator();
boolean fredPresent = false;
while (iter.hasNext()) {
Person pers = iter.next();
if (pers.getFirstName().equals("Fred") && pers.getLastName().equals("Flintstone") && pers.getPersonNum() == 101) {
fredPresent = true;
}
}
assertTrue("Fred was not returned!", fredPresent);
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.
the class CriteriaStringsTest method testProjectionOnManyToOne.
/**
* Test projection of many-to-one field.
*/
public void testProjectionOnManyToOne() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Farm> crit = cb.createQuery(Farm.class);
Root<Animal> candidate = crit.from(Animal.class);
candidate.alias("a");
Path<Farm> farmPath = candidate.get("farm");
crit.select(farmPath);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT a.farm FROM org.datanucleus.samples.annotations.one_many.bidir.Animal a", crit.toString());
Query q = em.createQuery(crit);
List<Farm> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 3, results.size());
Object result = results.get(0);
assertNotNull(result);
assertTrue("Result is of incorrect type", result instanceof Farm);
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.
the class CriteriaStringsTest method testSubqueryExists.
/**
* Test use of an EXISTS subquery.
*/
public void testSubqueryExists() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Person> crit = cb.createQuery(Person.class);
Root<Person> candidate = crit.from(Person.class);
candidate.alias("p");
crit.select(candidate);
Subquery<Person> subCrit = crit.subquery(Person.class);
Root<Person> subCandidate = subCrit.from(Person.class);
subCandidate.alias("p2");
subCrit.select(subCandidate);
Path bestFriendField = subCandidate.get("bestFriend");
Predicate bestFriendEqual = cb.equal(bestFriendField, candidate);
subCrit.where(bestFriendEqual);
Predicate existsBestFriend = cb.exists(subCrit);
crit.where(existsBestFriend);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT p2 FROM org.datanucleus.samples.annotations.models.company.Person p2 WHERE (p2.bestFriend = p)", subCrit.toString());
// Cannot check on precise query since the subquery name will be a random number
assertTrue("Main query containing subquery is incorrect", crit.toString().startsWith("SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE EXISTS "));
Query q = em.createQuery(crit);
List<Employee> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 1, results.size());
Person p = (Person) results.get(0);
assertEquals("Employee first name is incorrect", "Fred", p.getFirstName());
assertEquals("Employee last name is incorrect", "Flintstone", p.getLastName());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations