Search in sources :

Example 1 with Item

use of org.jpwh.model.collections.sortedsetofstrings.Item in project microservices by pwillhan.

the class SortedSetOfStrings method storeLoadCollection.

@Test
public void storeLoadCollection() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.getImages().add("foo.jpg");
        someItem.getImages().add("bar.jpg");
        someItem.getImages().add("baz.jpg");
        // Duplicate, filtered at Java level by HashSet!
        someItem.getImages().add("baz.jpg");
        em.persist(someItem);
        tx.commit();
        em.close();
        Long ITEM_ID = someItem.getId();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        assertEquals(item.getImages().size(), 3);
        // Sorted in-memory with TreeSet
        Iterator<String> it = item.getImages().iterator();
        String image;
        image = it.next();
        assertEquals(image, "bar.jpg");
        image = it.next();
        assertEquals(image, "baz.jpg");
        image = it.next();
        assertEquals(image, "foo.jpg");
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.collections.sortedsetofstrings.Item) EntityManager(javax.persistence.EntityManager) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)1 UserTransaction (javax.transaction.UserTransaction)1 JPATest (org.jpwh.env.JPATest)1 Item (org.jpwh.model.collections.sortedsetofstrings.Item)1 Test (org.testng.annotations.Test)1