Search in sources :

Example 86 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class MappedSubselect method storeItemAndBids.

public Long storeItemAndBids() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Item item = new Item();
    item.setName("Some item");
    item.setDescription("This is some description.");
    em.persist(item);
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid();
        bid.setAmount(new BigDecimal(10 + i));
        bid.setItem(item);
        em.persist(bid);
    }
    tx.commit();
    em.close();
    return item.getId();
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Bid(org.jpwh.model.advanced.Bid) BigDecimal(java.math.BigDecimal)

Example 87 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class MappedSubselect method loadSubselectEntity.

@Test
public void loadSubselectEntity() throws Exception {
    long ITEM_ID = storeItemAndBids();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        {
            ItemBidSummary itemBidSummary = em.find(ItemBidSummary.class, ITEM_ID);
            // select * from (
            // select i.ID as ITEMID, i.ITEM_NAME as NAME, ...
            // ) where ITEMID = ?
            assertEquals(itemBidSummary.getName(), "AUCTION: Some item");
        }
        em.clear();
        {
            // Hibernate will synchronize the right tables before querying
            Item item = em.find(Item.class, ITEM_ID);
            item.setName("New name");
            // No flush before retrieval by identifier!
            // ItemBidSummary itemBidSummary = em.find(ItemBidSummary.class, ITEM_ID);
            // Automatic flush before queries if synchronized tables are affected!
            Query query = em.createQuery("select ibs from ItemBidSummary ibs where ibs.itemId = :id");
            ItemBidSummary itemBidSummary = (ItemBidSummary) query.setParameter("id", ITEM_ID).getSingleResult();
            assertEquals(itemBidSummary.getName(), "AUCTION: New name");
        }
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) ItemBidSummary(org.jpwh.model.advanced.ItemBidSummary) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 88 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class NestedComponents method storeAndLoadUsers.

@Test
public void storeAndLoadUsers() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        City city = new City();
        city.setZipcode("12345");
        city.setName("Some City");
        city.setCountry(Locale.GERMANY.getCountry());
        Address address = new Address();
        address.setStreet("Some Street 123");
        address.setCity(city);
        User userOne = new User();
        userOne.setAddress(address);
        em.persist(userOne);
        tx.commit();
        em.close();
        tx.begin();
        em = JPA.createEntityManager();
        User u = em.find(User.class, userOne.getId());
        assertEquals(u.getAddress().getStreet(), "Some Street 123");
        assertEquals(u.getAddress().getCity().getZipcode(), "12345");
        assertEquals(u.getAddress().getCity().getCountry(), Locale.GERMANY.getCountry());
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.advanced.User) Address(org.jpwh.model.advanced.Address) City(org.jpwh.model.advanced.City) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 89 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class TransformingColumns method storeItemAndBids.

public Long storeItemAndBids() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Item item = new Item();
    item.setName("Some item");
    item.setMetricWeight(2);
    item.setDescription("This is some description.");
    em.persist(item);
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid();
        bid.setAmount(new BigDecimal(10 + i));
        bid.setItem(item);
        em.persist(bid);
    }
    tx.commit();
    em.close();
    return item.getId();
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Bid(org.jpwh.model.advanced.Bid) BigDecimal(java.math.BigDecimal)

Example 90 with UserTransaction

use of javax.transaction.UserTransaction 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

UserTransaction (javax.transaction.UserTransaction)642 EntityManager (javax.persistence.EntityManager)244 Test (org.testng.annotations.Test)187 Test (org.junit.Test)157 JPATest (org.jpwh.env.JPATest)138 InitialContext (javax.naming.InitialContext)62 BigDecimal (java.math.BigDecimal)53 HashMap (java.util.HashMap)52 IOException (java.io.IOException)50 Context (javax.naming.Context)49 List (java.util.List)47 FacesContext (javax.faces.context.FacesContext)43 NamingException (javax.naming.NamingException)43 NodeRef (org.alfresco.service.cmr.repository.NodeRef)43 Node (javax.jcr.Node)42 KieSession (org.kie.api.runtime.KieSession)42 Query (javax.persistence.Query)38 QueryingTest (org.jpwh.test.querying.QueryingTest)36 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)35 Item (org.jpwh.model.querying.Item)35