Search in sources :

Example 1 with Scientist

use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist in project eclipselink by eclipse-ee4j.

the class AdvancedCompositePKJunitTest method testAnyAndAll.

// bug gf672 - JPQL Select query with IN/ANY in WHERE clause and subselect fails.
public void testAnyAndAll() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Query query1 = em.createQuery("SELECT s FROM Scientist s WHERE s = ANY (SELECT s2 FROM Scientist s2)");
        List<Scientist> results1 = query1.getResultList();
        Query query2 = em.createQuery("SELECT s FROM Scientist s WHERE s = ALL (SELECT s2 FROM Scientist s2)");
        List<Scientist> results2 = query2.getResultList();
        Query query3 = em.createQuery("SELECT s FROM Scientist s WHERE s.department = ALL (SELECT DISTINCT d FROM Department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A')");
        List<Scientist> results3 = query3.getResultList();
        Query query4 = em.createQuery("SELECT s FROM Scientist s WHERE s.department = ANY (SELECT DISTINCT d FROM Department d JOIN d.scientists ds JOIN ds.cubicle c WHERE c.code = 'G')");
        List<Scientist> results4 = query4.getResultList();
        // control queries
        Query controlQuery1 = em.createQuery("SELECT s FROM Scientist s");
        List<Scientist> controlResults1 = controlQuery1.getResultList();
        List<Scientist> controlResults2;
        if (controlResults1.size() == 1) {
            controlResults2 = controlResults1;
        } else {
            controlResults2 = new ArrayList<Scientist>();
        }
        Query controlQuery3 = em.createQuery("SELECT s FROM Scientist s JOIN s.department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A'");
        List<Scientist> controlResults3 = controlQuery3.getResultList();
        Query controlQuery4 = em.createQuery("SELECT s FROM Scientist s WHERE EXISTS (SELECT DISTINCT d FROM Department d JOIN d.scientists ds JOIN ds.cubicle c WHERE c.code = 'G' AND d = s.department)");
        List<Scientist> controlResults4 = controlQuery4.getResultList();
        // compare results - they should be the same
        compareResults(results1, controlResults1, "query1");
        compareResults(results2, controlResults2, "query2");
        compareResults(results3, controlResults3, "query3");
        compareResults(results4, controlResults4, "query4");
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) Scientist(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist) JuniorScientist(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.JuniorScientist)

Example 2 with Scientist

use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist in project eclipselink by eclipse-ee4j.

the class JUnitJPQLComplexAggregateTestSuite method complexCountOnJoinedCompositePK.

/**
 * Test case bug 6155093:
 */
public void complexCountOnJoinedCompositePK() {
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        Scientist s = new Scientist();
        s.setFirstName("John");
        s.setLastName("Doe");
        Cubicle c = new Cubicle();
        c.setCode("G");
        c.setScientist(s);
        s.setCubicle(c);
        em.persist(c);
        em.persist(s);
        em.flush();
        // Need to create the expected result manually, because using the
        // TopLink query API would run into the same issue 6155093.
        List<Long> expectedResult = Arrays.asList(1L);
        Collections.sort(expectedResult);
        // COUNT DISTINCT with inner join
        String jpql = "SELECT COUNT(DISTINCT p) FROM Scientist e JOIN e.cubicle p WHERE e.lastName LIKE 'D%'";
        Query q = em.createQuery(jpql);
        List result = q.getResultList();
        Collections.sort(result);
        Assert.assertEquals("Complex COUNT on joined variable composite PK", expectedResult, result);
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(jakarta.persistence.Query) Scientist(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist) List(java.util.List) Cubicle(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Cubicle)

Example 3 with Scientist

use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist in project eclipselink by eclipse-ee4j.

the class AdvancedCompositePKJunitTest method testAnyAndAll.

// bug gf672 - JPQL Select query with IN/ANY in WHERE clause and subselect fails.
public void testAnyAndAll() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Scientist> cq = qb.createQuery(Scientist.class);
        Subquery sq = cq.subquery(Scientist.class);
        Root<Scientist> from_scientist = cq.from(Scientist.class);
        cq.where(qb.equal(from_scientist, qb.any(sq)));
        Query query1 = em.createQuery(cq);
        List<Scientist> results1 = query1.getResultList();
        // Query query2 = em.createQuery("SELECT s FROM Scientist s WHERE s = ALL (SELECT s2 FROM Scientist s2)");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        sq = cq.subquery(Scientist.class);
        from_scientist = cq.from(Scientist.class);
        cq.where(qb.equal(from_scientist, qb.all(sq)));
        Query query2 = em.createQuery(cq);
        List<Scientist> results2 = query2.getResultList();
        // Query query3 = em.createQuery("SELECT s FROM Scientist s WHERE s.department = ALL (SELECT DISTINCT d FROM Department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A')");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        sq = cq.subquery(Department.class);
        sq.distinct(true);
        Root<Department> from_department = sq.from(Department.class);
        sq.where(qb.and(qb.and(qb.equal(from_department.get("name"), "DEPT A"), qb.equal(from_department.get("role"), "ROLE A")), qb.equal(from_department.get("location"), "LOCATION A")));
        from_scientist = cq.from(Scientist.class);
        cq.where(qb.equal(from_scientist.get("department"), qb.all(sq)));
        Query query3 = em.createQuery(cq);
        List<Scientist> results3 = query3.getResultList();
        // Query query4 = em.createQuery("SELECT s FROM Scientist s WHERE s.department = ANY (SELECT DISTINCT d FROM Department d JOIN d.scientists ds JOIN ds.cubicle c WHERE c.code = 'G')");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        sq = cq.subquery(Department.class);
        sq.distinct(true);
        from_department = sq.from(Department.class);
        Join c = from_department.join("scientists").join("cubicle");
        sq.where(qb.equal(c.get("code"), "G"));
        from_scientist = cq.from(Scientist.class);
        cq.where(qb.equal(from_scientist.get("department"), qb.any(sq)));
        Query query4 = em.createQuery(cq);
        List<Scientist> results4 = query4.getResultList();
        // control queries
        // Query controlQuery1 = em.createQuery("SELECT s FROM Scientist s");
        Query controlQuery1 = em.createQuery(em.getCriteriaBuilder().createQuery(Scientist.class));
        List<Scientist> controlResults1 = controlQuery1.getResultList();
        List<Scientist> controlResults2;
        if (controlResults1.size() == 1) {
            controlResults2 = controlResults1;
        } else {
            controlResults2 = new ArrayList<Scientist>();
        }
        // Query controlQuery3 = em.createQuery("SELECT s FROM Scientist s JOIN s.department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A'");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        from_scientist = cq.from(Scientist.class);
        Join d = from_scientist.join("department");
        cq.where(qb.and(qb.and(qb.equal(d.get("name"), "DEPT A"), qb.equal(d.get("role"), "ROLE A")), qb.equal(d.get("location"), "LOCATION A")));
        Query controlQuery3 = em.createQuery(cq);
        List<Scientist> controlResults3 = controlQuery3.getResultList();
        // Query controlQuery4 = em.createQuery("SELECT s FROM Scientist s WHERE EXISTS (SELECT DISTINCT d FROM Department d JOIN d.scientists ds JOIN ds.cubicle c WHERE c.code = 'G' AND d = s.department)");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        sq = cq.subquery(Department.class);
        sq.distinct(true);
        from_department = sq.from(Department.class);
        from_scientist = cq.from(Scientist.class);
        c = from_department.join("scientists").join("cubicle");
        sq.where(qb.and(qb.equal(c.get("code"), "G"), qb.equal(from_department, from_scientist.get("department"))));
        cq.where(qb.exists(sq));
        Query controlQuery4 = em.createQuery(cq);
        List<Scientist> controlResults4 = controlQuery4.getResultList();
        // compare results - they should be the same
        compareResults(results1, controlResults1, "query1");
        compareResults(results2, controlResults2, "query2");
        compareResults(results3, controlResults3, "query3");
        compareResults(results4, controlResults4, "query4");
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) Query(jakarta.persistence.Query) Scientist(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist)

Example 4 with Scientist

use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist in project eclipselink by eclipse-ee4j.

the class AdvancedCompositePKJunitTest method testCreateScientists.

public void testCreateScientists() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Department department = em.merge(em.find(Department.class, m_departmentPK));
        Cubicle cubicle1 = new Cubicle("G");
        em.persist(cubicle1);
        Scientist scientist1 = new Scientist();
        scientist1.setFirstName("Guy");
        scientist1.setLastName("Pelletier");
        scientist1.setCubicle(cubicle1);
        department.addScientist(scientist1);
        em.persist(scientist1);
        Cubicle cubicle2 = new Cubicle("T");
        em.persist(cubicle2);
        Scientist scientist2 = new Scientist();
        scientist2.setFirstName("Tom");
        scientist2.setLastName("Ware");
        scientist2.setCubicle(cubicle2);
        department.addScientist(scientist2);
        em.persist(scientist2);
        Cubicle cubicle3 = new Cubicle("G");
        em.persist(cubicle3);
        Scientist scientist3 = new Scientist();
        scientist3.setFirstName("Gordon");
        scientist3.setLastName("Yorke");
        scientist3.setCubicle(cubicle3);
        department.addScientist(scientist3);
        em.persist(scientist3);
        Cubicle cubicle4 = new Cubicle("J");
        em.persist(cubicle4);
        JuniorScientist jScientist = new JuniorScientist();
        jScientist.setFirstName("Junior");
        jScientist.setLastName("Sao");
        jScientist.setCubicle(cubicle4);
        department.addScientist(jScientist);
        em.persist(jScientist);
        commitTransaction(em);
        m_scientist1PK = scientist1.getPK();
        m_scientist2PK = scientist2.getPK();
        m_scientist3PK = scientist3.getPK();
        m_jScientistPK = jScientist.getPK();
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) Scientist(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist) JuniorScientist(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.JuniorScientist) Cubicle(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Cubicle) JuniorScientist(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.JuniorScientist)

Aggregations

EntityManager (jakarta.persistence.EntityManager)4 Scientist (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist)4 Query (jakarta.persistence.Query)3 Cubicle (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Cubicle)2 Department (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department)2 JuniorScientist (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.JuniorScientist)2 List (java.util.List)1 ReportQuery (org.eclipse.persistence.queries.ReportQuery)1