Search in sources :

Example 1 with Item

use of org.eclipse.persistence.testing.models.jpa.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();
    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)

Example 2 with Item

use of org.eclipse.persistence.testing.models.jpa.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();
    try {
        beginTransaction(em);
        em.persist(cusClone1);
        em.persist(order1);
        commitTransaction(em);
        cusIDs[0] = cusClone1.getCustomerId();
        orderIDs[0] = order1.getOrderId();
        itemIDs[0] = item1.getItemId();
        clearCache();
        try {
            ServerSession ss = getServerSession();
            Vector vec = new Vector();
            vec.add(itemIDs[0]);
            list = (List) ss.executeQuery("findAllOrdersByItem", 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.relationships.Order) Item(org.eclipse.persistence.testing.models.jpa.relationships.Item) EntityManager(jakarta.persistence.EntityManager) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) Customer(org.eclipse.persistence.testing.models.jpa.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 3 with Item

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

the class RelationshipModelJUnitTestSuite method testModifyItem.

/**
 * Read an item, verify it contents, modify it and commit.
 */
public void testModifyItem() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Item item = em.find(Item.class, itemId);
        item.setName("Willy Waller");
        item.setDescription("For adults only!");
        assertTrue("The manufacturer was not persisted", item.getManufacturer() != null);
        assertTrue("The manufacturer of the item was incorrect", item.getManufacturer().getName().equals("Mattel Inc."));
        Lego lego = new Lego();
        lego.setName("The LEGO Group");
        item.setManufacturer(lego);
        assertTrue("The distributor was not persisted", item.getDistributor() != null);
        assertTrue("The distributor of the item was incorrect", item.getDistributor().getName().equals("Namco Games"));
        MegaBrands megaBrands = new MegaBrands();
        megaBrands.setName("MegaBrands Inc.");
        item.setDistributor(megaBrands);
        em.merge(item);
        em.persist(lego);
        em.persist(megaBrands);
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
    closeEntityManager(em);
}
Also used : Item(org.eclipse.persistence.testing.models.jpa.relationships.Item) EntityManager(jakarta.persistence.EntityManager) Lego(org.eclipse.persistence.testing.models.jpa.relationships.Lego) MegaBrands(org.eclipse.persistence.testing.models.jpa.relationships.MegaBrands)

Example 4 with Item

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

the class RelationshipModelJUnitTestSuite method testCreateItem.

/**
 * Create a new item that has a variable one to one to a manufacturer.
 */
public void testCreateItem() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Item item = new Item();
        item.setName("Synergizer2000");
        item.setDescription("Every kid must have one ... ");
        // Manufacturer does not cascade persist
        Mattel mattel = new Mattel();
        mattel.setName("Mattel Inc.");
        em.persist(mattel);
        item.setManufacturer(mattel);
        // Distributor will cascade persist
        Namco namco = new Namco();
        namco.setName("Namco Games");
        item.setDistributor(namco);
        em.persist(item);
        itemId = item.getItemId();
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
    closeEntityManager(em);
}
Also used : Item(org.eclipse.persistence.testing.models.jpa.relationships.Item) EntityManager(jakarta.persistence.EntityManager) Mattel(org.eclipse.persistence.testing.models.jpa.relationships.Mattel) Namco(org.eclipse.persistence.testing.models.jpa.relationships.Namco)

Example 5 with Item

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

the class RelationshipModelJUnitTestSuite method testVerifyItem.

/**
 * Verify the final contents of item.
 */
public void testVerifyItem() {
    EntityManager em = createEntityManager();
    Item item = em.find(Item.class, itemId);
    assertTrue("The manufacturer was not persisted", item.getManufacturer() != null);
    assertTrue("The manufacturer of the item was incorrect", item.getManufacturer().getName().equals("The LEGO Group"));
    assertTrue("The distributor was not persisted", item.getDistributor() != null);
    assertTrue("The distributor of the item was incorrect", item.getDistributor().getName().equals("MegaBrands Inc."));
    closeEntityManager(em);
}
Also used : Item(org.eclipse.persistence.testing.models.jpa.relationships.Item) EntityManager(jakarta.persistence.EntityManager)

Aggregations

EntityManager (jakarta.persistence.EntityManager)5 Item (org.eclipse.persistence.testing.models.jpa.relationships.Item)5 NoResultException (jakarta.persistence.NoResultException)2 NonUniqueResultException (jakarta.persistence.NonUniqueResultException)2 Customer (org.eclipse.persistence.testing.models.jpa.relationships.Customer)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 Lego (org.eclipse.persistence.testing.models.jpa.relationships.Lego)1 Mattel (org.eclipse.persistence.testing.models.jpa.relationships.Mattel)1 MegaBrands (org.eclipse.persistence.testing.models.jpa.relationships.MegaBrands)1