use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testSubqueryExistsBeforeAnd.
// cquery.where(qbuilder.and(qbuilder.exists(sq), isMale));
public void testSubqueryExistsBeforeAnd() {
EntityManager em = createEntityManager();
List<Employee> jpqlEmployees;
List<Employee> criteriaEmployees;
beginTransaction(em);
try {
jpqlEmployees = em.createQuery("SELECT e FROM Employee e WHERE EXISTS (SELECT p FROM e.projects p) AND e.gender = org.eclipse.persistence.testing.models.jpa.advanced.Employee.Gender.Male").getResultList();
em.clear();
CriteriaBuilder qbuilder = em.getCriteriaBuilder();
CriteriaQuery<Employee> cquery = qbuilder.createQuery(Employee.class);
Root<Employee> customer = cquery.from(Employee.class);
// create correlated subquery
Subquery<Project> sq = cquery.subquery(Project.class);
Root<Employee> sqc = sq.correlate(customer);
Path<Project> sqo = sqc.join("projects");
sq.select(sqo);
Predicate isMale = qbuilder.equal(customer.get("gender"), Employee.Gender.Male);
cquery.where(qbuilder.and(qbuilder.exists(sq), isMale));
TypedQuery<Employee> tquery = em.createQuery(cquery);
criteriaEmployees = tquery.getResultList();
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
compareIds(jpqlEmployees, criteriaEmployees);
for (Employee emp : criteriaEmployees) {
assertTrue("Found someone not male", emp.getGender() != null && emp.isMale());
assertTrue("Found someone without projects", !emp.getProjects().isEmpty());
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testSubqueryExistsAfterAnd.
// cquery.where(qbuilder.and(isMale, qbuilder.exists(sq)));
public void testSubqueryExistsAfterAnd() {
EntityManager em = createEntityManager();
List<Employee> jpqlEmployees;
List<Employee> criteriaEmployees;
beginTransaction(em);
try {
jpqlEmployees = em.createQuery("SELECT e FROM Employee e WHERE e.gender = org.eclipse.persistence.testing.models.jpa.advanced.Employee.Gender.Male AND EXISTS (SELECT p FROM e.projects p)").getResultList();
em.clear();
CriteriaBuilder qbuilder = em.getCriteriaBuilder();
CriteriaQuery<Employee> cquery = qbuilder.createQuery(Employee.class);
Root<Employee> customer = cquery.from(Employee.class);
// create correlated subquery
Subquery<Project> sq = cquery.subquery(Project.class);
Root<Employee> sqc = sq.correlate(customer);
Path<Project> sqo = sqc.join("projects");
sq.select(sqo);
Predicate isMale = qbuilder.equal(customer.get("gender"), Employee.Gender.Male);
cquery.where(qbuilder.and(isMale, qbuilder.exists(sq)));
TypedQuery<Employee> tquery = em.createQuery(cquery);
criteriaEmployees = tquery.getResultList();
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
compareIds(jpqlEmployees, criteriaEmployees);
for (Employee emp : criteriaEmployees) {
assertTrue("Found someone not male", emp.getGender() != null && emp.isMale());
assertTrue("Found someone without projects", !emp.getProjects().isEmpty());
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testSubqueryNotExistsAfterAnd.
// cquery.where(qbuilder.and(isFemale, qbuilder.not(qbuilder.exists(sq))));
public void testSubqueryNotExistsAfterAnd() {
EntityManager em = createEntityManager();
List<Employee> jpqlEmployees;
List<Employee> criteriaEmployees;
beginTransaction(em);
try {
jpqlEmployees = em.createQuery("SELECT e FROM Employee e WHERE e.gender = org.eclipse.persistence.testing.models.jpa.advanced.Employee.Gender.Female AND NOT EXISTS (SELECT p FROM e.projects p)").getResultList();
em.clear();
CriteriaBuilder qbuilder = em.getCriteriaBuilder();
CriteriaQuery<Employee> cquery = qbuilder.createQuery(Employee.class);
Root<Employee> customer = cquery.from(Employee.class);
// create correlated subquery
Subquery<Project> sq = cquery.subquery(Project.class);
Root<Employee> sqc = sq.correlate(customer);
Path<Project> sqo = sqc.join("projects");
sq.select(sqo);
Predicate isFemale = qbuilder.equal(customer.get("gender"), Employee.Gender.Female);
cquery.where(qbuilder.and(isFemale, qbuilder.not(qbuilder.exists(sq))));
TypedQuery<Employee> tquery = em.createQuery(cquery);
criteriaEmployees = tquery.getResultList();
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
compareIds(jpqlEmployees, criteriaEmployees);
for (Employee emp : criteriaEmployees) {
assertTrue("Found someone not female", emp.getGender() != null && emp.isFemale());
assertTrue("Found someone with projects", emp.getProjects().isEmpty());
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testSubqueryExistsNestedAfterLiteralAnd.
public void testSubqueryExistsNestedAfterLiteralAnd() {
EntityManager em = createEntityManager();
List<Employee> jpqlEmployees;
List<Employee> criteriaEmployees;
beginTransaction(em);
try {
jpqlEmployees = em.createQuery("SELECT e FROM Employee e join e.projects ep WHERE EXISTS (SELECT p FROM Project p WHERE ep = p AND EXISTS (SELECT t FROM Employee t WHERE p.teamLeader = t))").getResultList();
em.clear();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Employee> mainQuery = builder.createQuery(Employee.class);
Subquery<Object> subQuery1 = mainQuery.subquery(Object.class);
Subquery<Object> subQuery2 = subQuery1.subquery(Object.class);
Root<Employee> mainEmployee = mainQuery.from(Employee.class);
mainQuery.select(mainEmployee);
Root<Project> sub1Project = subQuery1.from(Project.class);
Join<Employee, Project> mainEmployeeProjects = mainEmployee.join("projects");
Root<Employee> sub2Employee = subQuery2.from(Employee.class);
Join<Employee, Employee> sub1ProjectTeamLeader = sub1Project.join("teamLeader");
subQuery2.where(builder.equal(sub2Employee, sub1ProjectTeamLeader));
Predicate oneEqualsOne = builder.equal(builder.literal(1), builder.literal(1));
subQuery1.where(builder.and(oneEqualsOne, builder.and(builder.exists(subQuery2), builder.equal(sub1Project, mainEmployeeProjects))));
Predicate twoEqualsTwo = builder.equal(builder.literal(2), builder.literal(2));
mainQuery.where(builder.and(twoEqualsTwo, builder.exists(subQuery1)));
TypedQuery<Employee> tquery = em.createQuery(mainQuery);
criteriaEmployees = tquery.getResultList();
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
compareIds(jpqlEmployees, criteriaEmployees);
for (Employee emp : criteriaEmployees) {
assertTrue("Found someone without projects", !emp.getProjects().isEmpty());
boolean atLeastOneProjectHasLeader = false;
for (Project proj : emp.getProjects()) {
if (proj.getTeamLeader() != null) {
atLeastOneProjectHasLeader = true;
break;
}
}
assertTrue("None of employee's projects has a leader", atLeastOneProjectHasLeader);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testAlternateSelection.
public void testAlternateSelection() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
em.createQuery("select p.teamLeader from Project p where p.name = 'Sales Reporting'").getResultList();
Metamodel mm = em.getMetamodel();
CriteriaBuilder qbuilder = em.getCriteriaBuilder();
CriteriaQuery<Employee> cquery = qbuilder.createQuery(Employee.class);
Root<Project> spouse = cquery.from(Project.class);
cquery.where(qbuilder.equal(spouse.get("name"), "Sales Reporting")).select(spouse.<Employee>get("teamLeader"));
TypedQuery<Employee> tquery = em.createQuery(cquery);
assertTrue("Did not find the correct leaders of Project Swirly Dirly.", tquery.getResultList().size() > 1);
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
Aggregations