Search in sources :

Example 1 with Item

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

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

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Item 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("fieldaccess");
    try {
        beginTransaction(em);
        em.persist(cusClone1);
        em.persist(cusClone2);
        commitTransaction(em);
        clearCache("fieldaccess");
        cusIDs[0] = cusClone1.getCustomerId();
        cusIDs[1] = cusClone2.getCustomerId();
        beginTransaction(em);
        try {
            returnedCustomer1 = (Customer) em.createNamedQuery("findAllCustomersFieldAccess").getSingleResult();
        } catch (NonUniqueResultException exceptionExpected1) {
            expectedException1 = exceptionExpected1;
        }
        try {
            // should be no Items to find, which should cause an
            // NoResultException
            Query query1 = em.createNamedQuery("findAllFieldAccessItemsByName");
            Item item = (Item) query1.setParameter(1, searchString).getSingleResult();
            item.toString();
        } catch (NoResultException exceptionExpected2) {
            expectedException2 = exceptionExpected2;
        }
        // bug 4301674 test
        EJBQueryImpl query2 = (EJBQueryImpl) em.createNamedQuery("findAllCustomersFieldAccess");
        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 FieldAccessCustomer 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 FieldAccessCustomer thecust WHERE thecust.customerId = :id");
        query4.setParameter("id", cusIDs[0]);
        ReadObjectQuery readObjectQuery = new ReadObjectQuery(Customer.class);
        readObjectQuery.setEJBQLString("SELECT OBJECT(thecust) FROM FieldAccessCustomer 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.fieldaccess.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.fieldaccess.relationships.Item) EntityManager(jakarta.persistence.EntityManager) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Item

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

the class RelationshipModelJUnitTestSuite method testNamedQueryWithArgumentsTest.

// Bug#4646580 Query arguments are added in EJBQL
public void testNamedQueryWithArgumentsTest() {
    Integer[] cusIDs = new Integer[3];
    Integer[] orderIDs = new Integer[3];
    Integer[] itemIDs = new Integer[3];
    Exception exception = null;
    List list = null;
    Customer cusClone1 = RelationshipsExamples.customerExample1();
    Item item1 = RelationshipsExamples.itemExample1();
    Order order1 = RelationshipsExamples.orderExample1();
    order1.setCustomer(cusClone1);
    order1.setItem(item1);
    EntityManager em = createEntityManager("fieldaccess");
    try {
        beginTransaction(em);
        em.persist(cusClone1);
        em.persist(order1);
        commitTransaction(em);
        cusIDs[0] = cusClone1.getCustomerId();
        orderIDs[0] = order1.getOrderId();
        itemIDs[0] = item1.getItemId();
        clearCache("fieldaccess");
        try {
            ServerSession ss = getServerSession("fieldaccess");
            Vector vec = new Vector();
            vec.add(itemIDs[0]);
            list = (List) ss.executeQuery("findAllFieldAccessOrdersByItem", vec);
        } catch (Exception ex) {
            exception = ex;
        }
        beginTransaction(em);
        Customer cus1 = em.find(Customer.class, cusIDs[0]);
        em.remove(cus1);
        Order ord1 = em.find(Order.class, orderIDs[0]);
        em.remove(ord1);
        Item it1 = em.find(Item.class, itemIDs[0]);
        em.remove(it1);
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
    if (exception != null) {
        fail("An exception is thrown: " + exception);
    }
    if (list.size() != 1) {
        fail("One order is expected but " + list.size() + " was returned");
    }
}
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) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) Customer(org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer) List(java.util.List) Vector(java.util.Vector) PersistenceException(jakarta.persistence.PersistenceException) QueryException(org.eclipse.persistence.exceptions.QueryException) NonUniqueResultException(jakarta.persistence.NonUniqueResultException) NoResultException(jakarta.persistence.NoResultException)

Example 5 with Item

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

the class RelationshipModelJUnitTestSuite method testReadCustomer.

// Test reading a customer works correctly.
public void testReadCustomer() {
    Customer customer = RelationshipsExamples.customerExample4();
    EntityManager em = createEntityManager("fieldaccess");
    QuerySQLTracker counter = null;
    try {
        beginTransaction(em);
        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);
        em.persist(customer);
        commitTransaction(em);
        closeEntityManager(em);
        clearCache("fieldaccess");
        em = createEntityManager("fieldaccess");
        beginTransaction(em);
        counter = new QuerySQLTracker(getServerSession("fieldaccess"));
        customer = em.find(Customer.class, customer.getCustomerId());
        for (Order order : customer.getOrders()) {
            order.getCustomer();
        }
        if (isWeavingEnabled("fieldaccess") && counter.getSqlStatements().size() > 2) {
            fail("Should have been 2 queries but was: " + counter.getSqlStatements().size());
        }
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        if (counter != null) {
            counter.remove();
        }
        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) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Aggregations

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