Search in sources :

Example 21 with Project

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());
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Predicate(jakarta.persistence.criteria.Predicate)

Example 22 with Project

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());
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Predicate(jakarta.persistence.criteria.Predicate)

Example 23 with Project

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());
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Predicate(jakarta.persistence.criteria.Predicate)

Example 24 with Project

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);
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Predicate(jakarta.persistence.criteria.Predicate) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee)

Example 25 with Project

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);
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Metamodel(jakarta.persistence.metamodel.Metamodel)

Aggregations

Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)65 EntityManager (jakarta.persistence.EntityManager)52 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)42 SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)39 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)38 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)31 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)25 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)19 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)18 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)18 List (java.util.List)15 ArrayList (java.util.ArrayList)14 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)12 Query (jakarta.persistence.Query)9 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)9 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)8 PersistenceException (jakarta.persistence.PersistenceException)6 Predicate (jakarta.persistence.criteria.Predicate)6 TypedQuery (jakarta.persistence.TypedQuery)5 HashSet (java.util.HashSet)5