Search in sources :

Example 1 with Bid

use of org.jpwh.model.fetching.cartesianproduct.Bid in project microservices by pwillhan.

the class CartesianProduct method fetchCollections.

@Test
public void fetchCollections() throws Exception {
    FetchTestData testData = storeTestData();
    loadEventListener.reset();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        long ITEM_ID = testData.items.getFirstId();
        Item item = em.find(Item.class, ITEM_ID);
        // select i.*, b.*, img.*
        // from ITEM i
        // left outer join BID b on b.ITEM_ID = i.ID
        // left outer join IMAGE img on img.ITEM_ID = i.ID
        // where i.ID = ?
        assertEquals(loadEventListener.getLoadCount(Item.class), 1);
        assertEquals(loadEventListener.getLoadCount(Bid.class), 3);
        em.detach(item);
        assertEquals(item.getImages().size(), 3);
        assertEquals(item.getBids().size(), 3);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.fetching.cartesianproduct.Item) EntityManager(javax.persistence.EntityManager) Bid(org.jpwh.model.fetching.cartesianproduct.Bid) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 2 with Bid

use of org.jpwh.model.fetching.cartesianproduct.Bid in project microservices by pwillhan.

the class CartesianProduct method storeTestData.

public FetchTestData storeTestData() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Long[] itemIds = new Long[3];
    Long[] userIds = new Long[3];
    User johndoe = new User("johndoe");
    em.persist(johndoe);
    userIds[0] = johndoe.getId();
    User janeroe = new User("janeroe");
    em.persist(janeroe);
    userIds[1] = janeroe.getId();
    User robertdoe = new User("robertdoe");
    em.persist(robertdoe);
    userIds[2] = robertdoe.getId();
    Item item = new Item("Item One", CalendarUtil.TOMORROW.getTime(), johndoe);
    item.getImages().add("foo.jpg");
    item.getImages().add("bar.jpg");
    item.getImages().add("baz.jpg");
    em.persist(item);
    itemIds[0] = item.getId();
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid(item, new BigDecimal(9 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item Two", CalendarUtil.TOMORROW.getTime(), johndoe);
    item.getImages().add("a.jpg");
    item.getImages().add("b.jpg");
    em.persist(item);
    itemIds[1] = item.getId();
    for (int i = 1; i <= 1; i++) {
        Bid bid = new Bid(item, new BigDecimal(2 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item Three", CalendarUtil.AFTER_TOMORROW.getTime(), janeroe);
    em.persist(item);
    itemIds[2] = item.getId();
    tx.commit();
    em.close();
    FetchTestData testData = new FetchTestData();
    testData.items = new TestData(itemIds);
    testData.users = new TestData(userIds);
    return testData;
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.fetching.cartesianproduct.Item) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.fetching.cartesianproduct.User) TestData(org.jpwh.shared.util.TestData) Bid(org.jpwh.model.fetching.cartesianproduct.Bid) BigDecimal(java.math.BigDecimal)

Aggregations

EntityManager (javax.persistence.EntityManager)2 UserTransaction (javax.transaction.UserTransaction)2 Bid (org.jpwh.model.fetching.cartesianproduct.Bid)2 Item (org.jpwh.model.fetching.cartesianproduct.Item)2 BigDecimal (java.math.BigDecimal)1 JPATest (org.jpwh.env.JPATest)1 User (org.jpwh.model.fetching.cartesianproduct.User)1 TestData (org.jpwh.shared.util.TestData)1 Test (org.testng.annotations.Test)1