Search in sources :

Example 1 with Customer

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer 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 2 with Customer

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer in project eclipselink by eclipse-ee4j.

the class RelationshipModelJUnitTestSuite method testUpdateCustomer.

// Test that updating a customer works correctly.
public void testUpdateCustomer() {
    Customer customer = RelationshipsExamples.customerExample4();
    EntityManager em = createEntityManager("fieldaccess");
    try {
        beginTransaction(em);
        em.persist(customer);
        commitTransaction(em);
        closeEntityManager(em);
        clearCache("fieldaccess");
        em = createEntityManager("fieldaccess");
        beginTransaction(em);
        customer = em.find(Customer.class, customer.getCustomerId());
        Item item1 = RelationshipsExamples.itemExample1();
        Order order1 = RelationshipsExamples.orderExample1();
        customer.addOrder(order1);
        order1.setItem(item1);
        em.flush();
        Item item2 = RelationshipsExamples.itemExample2();
        Order order2 = RelationshipsExamples.orderExample2();
        customer.addOrder(order2);
        order2.setItem(item2);
        Item item3 = RelationshipsExamples.itemExample3();
        Order order3 = RelationshipsExamples.orderExample3();
        customer.addOrder(order3);
        order3.setItem(item3);
        // Force LAZY otherwise compare will fail on was.
        getServerSession("fieldaccess").copy(customer);
        commitTransaction(em);
        beginTransaction(em);
        verifyObjectInEntityManager(customer, "fieldaccess");
        verifyObjectInEntityManager(order1, "fieldaccess");
        verifyObjectInEntityManager(item1, "fieldaccess");
        verifyObjectInEntityManager(order2, "fieldaccess");
        verifyObjectInEntityManager(item2, "fieldaccess");
        verifyObjectInEntityManager(order3, "fieldaccess");
        verifyObjectInEntityManager(item3, "fieldaccess");
        commitTransaction(em);
        clearCache("fieldaccess");
        beginTransaction(em);
        verifyObjectInEntityManager(customer, "fieldaccess");
        verifyObjectInEntityManager(order1, "fieldaccess");
        verifyObjectInEntityManager(item1, "fieldaccess");
        verifyObjectInEntityManager(order2, "fieldaccess");
        verifyObjectInEntityManager(item2, "fieldaccess");
        verifyObjectInEntityManager(order3, "fieldaccess");
        verifyObjectInEntityManager(item3, "fieldaccess");
        commitTransaction(em);
        clearCache("fieldaccess");
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : Order(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Order) Item(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Item) EntityManager(jakarta.persistence.EntityManager) Customer(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer)

Example 3 with Customer

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer in project eclipselink by eclipse-ee4j.

the class RelationshipModelJUnitTestSuite method testExecuteUpdateTest.

/*
     * Tests for the executeUpdate method on the EJBQueryImpl class.
     * Also tests bugs 4288845 and 4293920, that params are passed in and used correctly.
     */
public void testExecuteUpdateTest() {
    EntityManager em = createEntityManager("fieldaccess");
    Integer[] cusIDs = new Integer[2];
    String nameChange1 = "New Name1";
    String nameChange2 = "New Name2";
    String nameChange3 = "New Name3";
    String returnedName1, returnedName2, returnedName3;
    Exception expectedException = null;
    Exception expectedException2 = null;
    try {
        Customer cusClone1 = RelationshipsExamples.customerExample1();
        beginTransaction(em);
        em.persist(cusClone1);
        commitTransaction(em);
        em.clear();
        clearCache("fieldaccess");
        cusIDs[0] = cusClone1.getCustomerId();
        beginTransaction(em);
        Customer cus = em.find(Customer.class, cusIDs[0]);
        Query query = em.createQuery("UPDATE FieldAccessCustomer customer SET customer.name = '" + nameChange1 + "' WHERE customer.customerId = " + cusIDs[0]);
        query.executeUpdate();
        em.clear();
        clearCache("fieldaccess");
        // getEntityManager().refresh(cus);
        cus = em.find(Customer.class, cusIDs[0]);
        returnedName1 = cus.getName();
        // tests bug 4288845
        Query query2 = em.createQuery("UPDATE FieldAccessCustomer customer SET customer.name = :name WHERE customer.customerId = " + cusIDs[0]);
        query2.setParameter("name", nameChange2);
        query2.executeUpdate();
        em.clear();
        clearCache("fieldaccess");
        // getEntityManager().refresh(cus);
        cus = em.find(Customer.class, cusIDs[0]);
        returnedName2 = cus.getName();
        // tests bug 4293920
        Query query3 = em.createQuery("UPDATE FieldAccessCustomer customer SET customer.name = :name WHERE customer.customerId = :id");
        query3.setParameter("name", nameChange3);
        query3.setParameter("id", cusIDs[0]);
        query3.executeUpdate();
        em.clear();
        clearCache("fieldaccess");
        // getEntityManager().refresh(cus);
        cus = em.find(Customer.class, cusIDs[0]);
        returnedName3 = cus.getName();
        // tests bug 4294241
        try {
            Query query4 = em.createNamedQuery("findAllCustomersFieldAccess");
            query4.executeUpdate();
        } catch (IllegalStateException expected) {
            expectedException = expected;
        }
        expectedException2 = null;
        try {
            commitTransaction(em);
            expectedException2 = null;
        } catch (Exception ex) {
            expectedException2 = ex;
        }
        if ((returnedName1 == null || !returnedName1.equals(nameChange1))) {
            fail("Customer name did not get updated correctly should be:" + nameChange1 + " is :" + returnedName1);
        }
        if ((returnedName2 == null || !returnedName2.equals(nameChange2))) {
            fail("Customer name did not get updated correctly should be:" + nameChange2 + " is :" + returnedName2);
        }
        if ((returnedName3 == null || !returnedName3.equals(nameChange3))) {
            fail("Customer name did not get updated correctly should be:" + nameChange3 + " is :" + returnedName3);
        }
        if (expectedException == null) {
            fail("excuteUpdate did not result in an exception on findAllCustomersFieldAccess named ReadAllQuery");
        }
        if (expectedException2 == null) {
            fail("commit did not throw expected RollbackException");
        }
        beginTransaction(em);
        Customer cus1 = em.find(Customer.class, cusIDs[0]);
        em.remove(cus1);
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) Customer(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer) PersistenceException(jakarta.persistence.PersistenceException) QueryException(org.eclipse.persistence.exceptions.QueryException) NonUniqueResultException(jakarta.persistence.NonUniqueResultException) NoResultException(jakarta.persistence.NoResultException)

Example 4 with Customer

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer in project eclipselink by eclipse-ee4j.

the class RelationshipModelJUnitTestSuite method testPersistCustomer.

// Test that persisting a customer works correctly.
public void testPersistCustomer() {
    Customer customer = RelationshipsExamples.customerExample4();
    Item item1 = RelationshipsExamples.itemExample1();
    Order order1 = RelationshipsExamples.orderExample1();
    customer.addOrder(order1);
    order1.setItem(item1);
    Item item2 = RelationshipsExamples.itemExample2();
    Order order2 = RelationshipsExamples.orderExample2();
    customer.addOrder(order2);
    order2.setItem(item2);
    Item item3 = RelationshipsExamples.itemExample3();
    Order order3 = RelationshipsExamples.orderExample3();
    customer.addOrder(order3);
    order3.setItem(item3);
    EntityManager em = createEntityManager("fieldaccess");
    try {
        beginTransaction(em);
        em.persist(customer);
        commitTransaction(em);
        beginTransaction(em);
        verifyObjectInEntityManager(customer, "fieldaccess");
        verifyObjectInEntityManager(order1, "fieldaccess");
        verifyObjectInEntityManager(order2, "fieldaccess");
        verifyObjectInEntityManager(item1, "fieldaccess");
        verifyObjectInEntityManager(item2, "fieldaccess");
        commitTransaction(em);
        clearCache("fieldaccess");
        beginTransaction(em);
        verifyObjectInEntityManager(customer, "fieldaccess");
        verifyObjectInEntityManager(order1, "fieldaccess");
        verifyObjectInEntityManager(order2, "fieldaccess");
        verifyObjectInEntityManager(item1, "fieldaccess");
        verifyObjectInEntityManager(item2, "fieldaccess");
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : Order(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Order) Item(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Item) EntityManager(jakarta.persistence.EntityManager) Customer(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer)

Example 5 with Customer

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer 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)

Aggregations

EntityManager (jakarta.persistence.EntityManager)9 Customer (org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer)9 Item (org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Item)6 Order (org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Order)5 PersistenceException (jakarta.persistence.PersistenceException)4 QueryException (org.eclipse.persistence.exceptions.QueryException)4 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)4 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)4 NoResultException (jakarta.persistence.NoResultException)3 NonUniqueResultException (jakarta.persistence.NonUniqueResultException)3 Query (jakarta.persistence.Query)3 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)3 MapContainerPolicy (org.eclipse.persistence.internal.queries.MapContainerPolicy)3 Collection (java.util.Collection)2 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Vector (java.util.Vector)1 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)1