Search in sources :

Example 6 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class CriteriaQueryTestSuite method testCriteriaDeleteCompareSQL.

public void testCriteriaDeleteCompareSQL() {
    EntityManager em = createEntityManager();
    JpaEntityManager jpaEM = JpaHelper.getEntityManager((EntityManager) em.getDelegate());
    EJBQueryImpl query = (EJBQueryImpl) jpaEM.createQuery("DELETE FROM PhoneNumber phone where phone.owner.firstName is not null");
    String baseSQL = query.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    List baseSQLStrings = query.getDatabaseQuery().getTranslatedSQLStrings(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    // test query "Delete Employee e where e.firstName is not null";
    CriteriaBuilder qb = jpaEM.getCriteriaBuilder();
    CriteriaDelete<PhoneNumber> cq = qb.createCriteriaDelete(PhoneNumber.class);
    Root<PhoneNumber> root = cq.from(PhoneNumber.class);
    cq.where(qb.isNotNull(root.get("owner").get("firstName")));
    EJBQueryImpl testQuery = (EJBQueryImpl) jpaEM.createQuery(cq);
    String testSQL = testQuery.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    List testSQLStrings = testQuery.getDatabaseQuery().getTranslatedSQLStrings(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    closeEntityManager(em);
    if (testSQL != null) {
        assertEquals("Delete Criteria query did not match SQL used for a JPQL query; generated SQL was: \"" + testSQL + "\"  but we expected: \"" + baseSQL + "\"", testSQL, baseSQL);
    } else {
        // check list of strings instead
        boolean pass = true;
        if (testSQLStrings == null || baseSQLStrings == null || testSQLStrings.size() != baseSQLStrings.size()) {
            pass = false;
        } else {
            List clonedBaseStrings = new java.util.ArrayList(baseSQLStrings);
            for (String testSQLString : (List<String>) testSQLStrings) {
                if (clonedBaseStrings.contains(testSQLString)) {
                    clonedBaseStrings.remove(testSQLString);
                } else {
                    pass = false;
                    break;
                }
            }
            if (clonedBaseStrings.size() != 0) {
                pass = false;
            }
        }
        assertTrue("Delete Criteria query translated strings did not match JPQL query; criteria generated " + testSQLStrings + " but JPQL generated " + baseSQLStrings, pass);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) PhoneNumber(org.eclipse.persistence.testing.models.jpa21.advanced.PhoneNumber) List(java.util.List)

Example 7 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class CriteriaQueryTestSuite method testCriteriaUpdateCompareSQL.

public void testCriteriaUpdateCompareSQL() {
    if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) {
        getPersistenceUnitServerSession().logMessage("Test simpleUpdate skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    EntityManager em = createEntityManager();
    JpaEntityManager jpaEM = JpaHelper.getEntityManager((EntityManager) em.getDelegate());
    EJBQueryImpl query = (EJBQueryImpl) jpaEM.createQuery("UPDATE Employee e SET e.firstName = 'CHANGED' where e.firstName is not null");
    String baseSQL = query.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    List baseSQLStrings = query.getDatabaseQuery().getTranslatedSQLStrings(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    // test query "UPDATE Employee e SET e.firstName = 'CHANGED'";
    CriteriaBuilder qb = jpaEM.getCriteriaBuilder();
    CriteriaUpdate<Employee> cq = qb.createCriteriaUpdate(Employee.class);
    Root<Employee> root = cq.from(Employee.class);
    cq.set(root.get("firstName"), "CHANGED");
    cq.where(qb.isNotNull(root.get("firstName")));
    EJBQueryImpl testQuery = (EJBQueryImpl) jpaEM.createQuery(cq);
    String testSQL = testQuery.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    List testSQLStrings = testQuery.getDatabaseQuery().getTranslatedSQLStrings(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    closeEntityManager(em);
    if (testSQL != null) {
        assertEquals("UPDATE Criteria query did not match SQL used for a JPQL query; generated SQL was: \"" + testSQL + "\"  but we expected: \"" + baseSQL + "\"", testSQL, baseSQL);
    } else {
        // check list of strings instead
        boolean pass = true;
        if (testSQLStrings == null || baseSQLStrings == null || testSQLStrings.size() != baseSQLStrings.size()) {
            pass = false;
        } else {
            List clonedBaseStrings = new java.util.ArrayList(baseSQLStrings);
            for (String testSQLString : (List<String>) testSQLStrings) {
                if (clonedBaseStrings.contains(testSQLString)) {
                    clonedBaseStrings.remove(testSQLString);
                } else {
                    pass = false;
                    break;
                }
            }
            if (clonedBaseStrings.size() != 0) {
                pass = false;
            }
        }
        assertTrue("UPDATE Criteria query translated strings did not match JPQL query; criteria generated " + testSQLStrings + " but JPQL generated " + baseSQLStrings, pass);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa21.advanced.Employee) List(java.util.List)

Example 8 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class RelationshipModelJUnitTestSuite method testGetResultListTest.

/*
     * Tests using the 'getSingleResult' api on a Query object obtained from the
     * EntityManager Also tests bugs 4300879 - check non Collection container
     * policy error and 4297903 - check ReadObjectQuery fails
     */
public void testGetResultListTest() {
    Collection returnedCustomers1, returnedCustomers2;
    QueryException expectedException1 = null;
    String ejbql1 = "SELECT OBJECT(thecust) FROM FieldAccessCustomer thecust WHERE thecust.customerId = :id";
    Integer[] cusIDs = new Integer[3];
    Customer cusClone1 = RelationshipsExamples.customerExample1();
    Customer cusClone2 = RelationshipsExamples.customerExample2();
    EntityManager em = createEntityManager("fieldaccess");
    beginTransaction(em);
    em.persist(cusClone1);
    em.persist(cusClone2);
    commitTransaction(em);
    em.clear();
    clearCache("fieldaccess");
    cusIDs[0] = cusClone1.getCustomerId();
    cusIDs[1] = cusClone2.getCustomerId();
    try {
        beginTransaction(em);
        EntityManagerImpl entityManagerImpl = (EntityManagerImpl) em.getDelegate();
        Query query1 = em.createNamedQuery("findAllCustomersFieldAccess");
        returnedCustomers1 = query1.getResultList();
        Query query2 = em.createQuery(ejbql1);
        query2.setParameter("id", -10);
        returnedCustomers2 = query2.getResultList();
        // bug:4297903, check container policy failure
        EJBQueryImpl query3 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
        ReadAllQuery readAllQuery = new ReadAllQuery(Customer.class);
        MapContainerPolicy mapContainerPolicy = new MapContainerPolicy();
        mapContainerPolicy.setContainerClass(HashMap.class);
        mapContainerPolicy.setKeyName("hashCode");
        readAllQuery.setContainerPolicy(mapContainerPolicy);
        query3.setDatabaseQuery(readAllQuery);
        try {
            query3.getResultList();
        } catch (PersistenceException exc) {
            // QueryException.INVALID_CONTAINER_CLASS
            expectedException1 = (QueryException) exc.getCause();
            rollbackTransaction(em);
            beginTransaction(em);
        }
        entityManagerImpl = (EntityManagerImpl) em.getDelegate();
        // bug:4300879, check ReadObjectQuery fails
        EJBQueryImpl query4 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
        query4.setParameter("id", -10);
        ReadObjectQuery readObjectQuery2 = new ReadObjectQuery(Customer.class);
        readObjectQuery2.setEJBQLString(ejbql1);
        query4.setDatabaseQuery(readObjectQuery2);
        query4.getResultList();
        commitTransaction(em);
        if (returnedCustomers1 == null || (returnedCustomers1.size() < 2)) {
            fail("Not all customers were returned from findAllCustomers query ");
        }
        if (returnedCustomers2 == null || (returnedCustomers2.size() != 0)) {
            fail("Customer from ReadObjectQuery was not returned using getResultCollection");
        }
        if (expectedException1 == null || (expectedException1.getErrorCode() != QueryException.INVALID_CONTAINER_CLASS)) {
            fail("getResultCollection on query returning a hashtable did not throw expected INVALID_CONTAINER_CLASS QueryException");
        }
        beginTransaction(em);
        Customer cus1 = em.find(Customer.class, cusIDs[0]);
        em.remove(cus1);
        Customer cus2 = em.find(Customer.class, cusIDs[1]);
        em.remove(cus2);
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
}
Also used : Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) Customer(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) MapContainerPolicy(org.eclipse.persistence.internal.queries.MapContainerPolicy) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) QueryException(org.eclipse.persistence.exceptions.QueryException) EntityManager(jakarta.persistence.EntityManager) PersistenceException(jakarta.persistence.PersistenceException) Collection(java.util.Collection)

Example 9 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class RelationshipModelJUnitTestSuite method testGetResultCollectionTest.

/*
     * Tests using the 'getResultCollection' api on a Query object obtained from the
     * EntityManager Also tests bugs 4300879 - check non Collection container
     * policy error and 4297903 - check ReadObjectQuery fails
     */
public void testGetResultCollectionTest() {
    Collection returnedCustomers1, returnedCustomers2;
    QueryException expectedException1 = null;
    String ejbql1 = "SELECT OBJECT(thecust) FROM FieldAccessCustomer thecust WHERE thecust.customerId = :id";
    Integer[] cusIDs = new Integer[3];
    Customer cusClone1 = RelationshipsExamples.customerExample1();
    Customer cusClone2 = RelationshipsExamples.customerExample2();
    EntityManager em = createEntityManager("fieldaccess");
    beginTransaction(em);
    em.persist(cusClone1);
    em.persist(cusClone2);
    commitTransaction(em);
    em.clear();
    clearCache("fieldaccess");
    cusIDs[0] = cusClone1.getCustomerId();
    cusIDs[1] = cusClone2.getCustomerId();
    try {
        beginTransaction(em);
        EntityManagerImpl entityManagerImpl = (EntityManagerImpl) em.getDelegate();
        EJBQueryImpl query1 = (EJBQueryImpl) entityManagerImpl.createNamedQuery("findAllCustomersFieldAccess");
        returnedCustomers1 = query1.getResultCollection();
        EJBQueryImpl query2 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
        query2.setParameter("id", -10);
        returnedCustomers2 = query2.getResultCollection();
        // bug:4297903, check container policy failure
        EJBQueryImpl query3 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
        ReadAllQuery readAllQuery = new ReadAllQuery(Customer.class);
        MapContainerPolicy mapContainerPolicy = new MapContainerPolicy();
        mapContainerPolicy.setContainerClass(HashMap.class);
        mapContainerPolicy.setKeyName("hashCode");
        readAllQuery.setContainerPolicy(mapContainerPolicy);
        query3.setDatabaseQuery(readAllQuery);
        try {
            query3.getResultCollection();
        } catch (PersistenceException exc) {
            // QueryException.INVALID_CONTAINER_CLASS
            expectedException1 = (QueryException) exc.getCause();
            rollbackTransaction(em);
            beginTransaction(em);
        }
        entityManagerImpl = (EntityManagerImpl) em.getDelegate();
        // bug:4300879, check ReadObjectQuery fails
        EJBQueryImpl query4 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
        query4.setParameter("id", -10);
        ReadObjectQuery readObjectQuery2 = new ReadObjectQuery(Customer.class);
        readObjectQuery2.setEJBQLString(ejbql1);
        query4.setDatabaseQuery(readObjectQuery2);
        query4.getResultCollection();
        commitTransaction(em);
        if (returnedCustomers1 == null || (returnedCustomers1.size() < 2)) {
            fail("Not all customers were returned from findAllCustomers query ");
        }
        if (returnedCustomers2 == null || (returnedCustomers2.size() != 0)) {
            fail("Customer from ReadObjectQuery was not returned using getResultCollection");
        }
        if (expectedException1 == null || (expectedException1.getErrorCode() != QueryException.INVALID_CONTAINER_CLASS)) {
            fail("getResultCollection on query returning a hashtable did not throw expected INVALID_CONTAINER_CLASS QueryException");
        }
        beginTransaction(em);
        Customer cus1 = em.find(Customer.class, cusIDs[0]);
        em.remove(cus1);
        Customer cus2 = em.find(Customer.class, cusIDs[1]);
        em.remove(cus2);
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
}
Also used : ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) Customer(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) MapContainerPolicy(org.eclipse.persistence.internal.queries.MapContainerPolicy) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) QueryException(org.eclipse.persistence.exceptions.QueryException) EntityManager(jakarta.persistence.EntityManager) PersistenceException(jakarta.persistence.PersistenceException) Collection(java.util.Collection)

Example 10 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class RelationshipModelJUnitTestSuite method testGetSingleResultTest.

/*
     * Tests using the 'getSingleResult' api on a Query object obtained from the
     * EntityManager Tests fixes for bugs 4202835 and 4301674
     *
     * modified for changes in bug:4628215 (EntityNotFoundException)
     * EntityNotFoundException changed to NoResultException as per new spec
     */
public void testGetSingleResultTest() {
    // used for verification
    Customer returnedCustomer1, returnedCustomer2 = null;
    NonUniqueResultException expectedException1 = null;
    NoResultException expectedException2 = null;
    String searchString = "notAnItemName";
    Integer[] cusIDs = new Integer[3];
    Customer cusClone1 = RelationshipsExamples.customerExample1();
    Customer cusClone2 = RelationshipsExamples.customerExample2();
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        em.persist(cusClone1);
        em.persist(cusClone2);
        commitTransaction(em);
        clearCache();
        cusIDs[0] = cusClone1.getCustomerId();
        cusIDs[1] = cusClone2.getCustomerId();
        beginTransaction(em);
        try {
            returnedCustomer1 = (Customer) em.createNamedQuery("findAllCustomers").getSingleResult();
        } catch (NonUniqueResultException exceptionExpected1) {
            expectedException1 = exceptionExpected1;
        }
        try {
            // should be no Items to find, which should cause an
            // NoResultException
            Query query1 = em.createNamedQuery("findAllItemsByName");
            Item item = (Item) query1.setParameter(1, searchString).getSingleResult();
            item.toString();
        } catch (NoResultException exceptionExpected2) {
            expectedException2 = exceptionExpected2;
        }
        // bug 4301674 test
        EJBQueryImpl query2 = (EJBQueryImpl) em.createNamedQuery("findAllCustomers");
        ReadAllQuery readAllQuery = new ReadAllQuery(Customer.class);
        MapContainerPolicy mapContainerPolicy = new MapContainerPolicy();
        mapContainerPolicy.setContainerClass(HashMap.class);
        mapContainerPolicy.setKeyName("hashCode");
        readAllQuery.setContainerPolicy(mapContainerPolicy);
        query2.setDatabaseQuery(readAllQuery);
        Map result = (Map) query2.getSingleResult();
        result.toString();
        // check for single result found.
        Query query3 = em.createQuery("SELECT OBJECT(thecust) FROM Customer thecust WHERE thecust.customerId = :id");
        returnedCustomer1 = (Customer) query3.setParameter("id", cusIDs[0]).getSingleResult();
        // check for single result using a ReadObjectQuery (tests previous
        // fix for 4202835)
        EJBQueryImpl query4 = (EJBQueryImpl) em.createQuery("SELECT OBJECT(thecust) FROM Customer thecust WHERE thecust.customerId = :id");
        query4.setParameter("id", cusIDs[0]);
        ReadObjectQuery readObjectQuery = new ReadObjectQuery(Customer.class);
        readObjectQuery.setEJBQLString("SELECT OBJECT(thecust) FROM Customer thecust WHERE thecust.customerId = :id");
        query4.setDatabaseQuery(readObjectQuery);
        returnedCustomer2 = (Customer) query4.getSingleResult();
        commitTransaction(em);
        beginTransaction(em);
        Customer cus1 = em.find(Customer.class, cusIDs[0]);
        em.remove(cus1);
        Customer cus2 = em.find(Customer.class, cusIDs[1]);
        em.remove(cus2);
        commitTransaction(em);
        if (expectedException1 == null) {
            fail("getSingelResult on query returning multiple values did not throw a NonUniqueResultException");
        }
        if (expectedException2 == null) {
            fail("getSingelResult on query returning multiple values did not throw an NoResultException");
        }
        if (returnedCustomer1 == null || (!returnedCustomer1.getCustomerId().equals(cusIDs[0]))) {
            fail("Incorrect Single Customer returned, found: " + returnedCustomer1);
        }
        if (returnedCustomer2 == null || (!returnedCustomer2.getCustomerId().equals(cusIDs[0]))) {
            fail("Incorrect Single Customer returned, found: " + returnedCustomer2);
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
}
Also used : NonUniqueResultException(jakarta.persistence.NonUniqueResultException) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) Customer(org.eclipse.persistence.testing.models.jpa.relationships.Customer) MapContainerPolicy(org.eclipse.persistence.internal.queries.MapContainerPolicy) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) NoResultException(jakarta.persistence.NoResultException) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) Item(org.eclipse.persistence.testing.models.jpa.relationships.Item) EntityManager(jakarta.persistence.EntityManager) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)37 EntityManager (jakarta.persistence.EntityManager)31 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)19 Query (jakarta.persistence.Query)16 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)15 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)12 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)8 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)8 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)6 MapContainerPolicy (org.eclipse.persistence.internal.queries.MapContainerPolicy)6 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)6 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)6 List (java.util.List)5 PersistenceException (jakarta.persistence.PersistenceException)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 QueryException (org.eclipse.persistence.exceptions.QueryException)4 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)4 ReportQuery (org.eclipse.persistence.queries.ReportQuery)4