Search in sources :

Example 1 with Item

use of org.jpwh.model.associations.manytomany.ternary.Item in project microservices by pwillhan.

the class ManyToManyTernary method storeLoadCategoryItems.

@Test
public void storeLoadCategoryItems() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Category someCategory = new Category("Some Category");
        Category otherCategory = new Category("Other Category");
        em.persist(someCategory);
        em.persist(otherCategory);
        Item someItem = new Item("Some Item");
        Item otherItem = new Item("Other Item");
        em.persist(someItem);
        em.persist(otherItem);
        User someUser = new User("johndoe");
        em.persist(someUser);
        CategorizedItem linkOne = new CategorizedItem(someUser, someItem);
        someCategory.getCategorizedItems().add(linkOne);
        CategorizedItem linkTwo = new CategorizedItem(someUser, otherItem);
        someCategory.getCategorizedItems().add(linkTwo);
        CategorizedItem linkThree = new CategorizedItem(someUser, someItem);
        otherCategory.getCategorizedItems().add(linkThree);
        tx.commit();
        em.close();
        Long CATEGORY_ID = someCategory.getId();
        Long OTHER_CATEGORY_ID = otherCategory.getId();
        Long ITEM_ID = someItem.getId();
        Long USER_ID = someUser.getId();
        tx.begin();
        em = JPA.createEntityManager();
        Category category1 = em.find(Category.class, CATEGORY_ID);
        Category category2 = em.find(Category.class, OTHER_CATEGORY_ID);
        Item item1 = em.find(Item.class, ITEM_ID);
        User user = em.find(User.class, USER_ID);
        assertEquals(category1.getCategorizedItems().size(), 2);
        assertEquals(category2.getCategorizedItems().size(), 1);
        assertEquals(category2.getCategorizedItems().iterator().next().getItem(), item1);
        assertEquals(category2.getCategorizedItems().iterator().next().getAddedBy(), user);
        tx.commit();
        em.close();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        List<Category> categoriesOfItem = em.createQuery("select c from Category c " + "join c.categorizedItems ci " + "where ci.item = :itemParameter").setParameter("itemParameter", item).getResultList();
        assertEquals(categoriesOfItem.size(), 2);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) CategorizedItem(org.jpwh.model.associations.manytomany.ternary.CategorizedItem) Item(org.jpwh.model.associations.manytomany.ternary.Item) EntityManager(javax.persistence.EntityManager) Category(org.jpwh.model.associations.manytomany.ternary.Category) User(org.jpwh.model.associations.manytomany.ternary.User) CategorizedItem(org.jpwh.model.associations.manytomany.ternary.CategorizedItem) 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 CategorizedItem (org.jpwh.model.associations.manytomany.ternary.CategorizedItem)1 Category (org.jpwh.model.associations.manytomany.ternary.Category)1 Item (org.jpwh.model.associations.manytomany.ternary.Item)1 User (org.jpwh.model.associations.manytomany.ternary.User)1 Test (org.testng.annotations.Test)1