use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFromJoinOneToOne.
/**
* Test basic generation of query with candidate and alias, and FROM join on 1-1.
*/
public void testBasicWithFromJoinOneToOne() {
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);
Metamodel model = emf.getMetamodel();
ManagedType empType = model.managedType(Employee.class);
Attribute bAttr = empType.getAttribute("account");
Join accountJoin = candidate.join((SingularAttribute) bAttr);
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();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFilterOr.
/**
* Test basic generation of query with candidate and alias and filter (using OR).
*/
public void testBasicWithFilterOr() {
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");
Predicate firstName1 = cb.equal(firstNameField, "Fred");
Predicate firstName2 = cb.equal(firstNameField, "Joe");
Predicate eitherFirstName = cb.or(firstName1, firstName2);
crit.where(eitherFirstName);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE" + " ((p.firstName = 'Fred') OR (p.firstName = 'Joe'))", crit.toString());
Query q = em.createQuery(crit);
List<Person> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 2, results.size());
Iterator<Person> iter = results.iterator();
boolean fredPresent = false;
boolean joePresent = false;
while (iter.hasNext()) {
Person pers = iter.next();
if (pers.getFirstName().equals("Fred") && pers.getLastName().equals("Flintstone") && pers.getPersonNum() == 101) {
fredPresent = true;
} else if (pers.getFirstName().equals("Joe") && pers.getLastName().equals("Bloggs") && pers.getPersonNum() == 105) {
joePresent = true;
}
}
assertTrue("Fred was not returned!", fredPresent);
assertTrue("Joe was not returned!", joePresent);
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 testBasic.
/**
* Test basic generation of query with candidate and alias.
*/
public void testBasic() {
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);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p", 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 testFilterWithIn.
/**
* Test generation of filter with IN.
*/
public void testFilterWithIn() {
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);
List<String> nameOptions = new ArrayList<String>();
nameOptions.add("Fred");
nameOptions.add("George");
Path firstNameField = candidate.get("firstName");
Predicate nameIn = firstNameField.in(nameOptions);
crit.where(nameIn);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE p.firstName IN ('Fred','George')", 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 testBasicWithResult.
/**
* Test basic generation of query with result.
*/
public void testBasicWithResult() {
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");
Path firstNameField = candidate.get("firstName");
Path lastNameField = candidate.get("lastName");
crit.multiselect(firstNameField, lastNameField);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT p.firstName,p.lastName FROM org.datanucleus.samples.annotations.models.company.Person p", crit.toString());
Query q = em.createQuery(crit);
List<Object> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 4, results.size());
Iterator<Object> iter = results.iterator();
boolean fredExists = false;
boolean barneyExists = false;
boolean joeExists = false;
boolean nigelExists = false;
while (iter.hasNext()) {
Object obj = iter.next();
assertTrue("Row of results is not Object[]", obj instanceof Object[]);
Object[] row = (Object[]) obj;
if (row[0].equals("Fred") && row[1].equals("Flintstone")) {
fredExists = true;
} else if (row[0].equals("Barney") && row[1].equals("Rubble")) {
barneyExists = true;
} else if (row[0].equals("Joe") && row[1].equals("Bloggs")) {
joeExists = true;
} else if (row[0].equals("Nigel") && row[1].equals("Bloggs")) {
nigelExists = true;
}
}
assertTrue("Fred wasnt in the results", fredExists);
assertTrue("Barney wasnt in the results", barneyExists);
assertTrue("Joe wasnt in the results", joeExists);
assertTrue("Nigel wasnt in the results", nigelExists);
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations