Search in sources :

Example 1 with Item

use of org.jpwh.model.fetching.readonly.Item in project microservices by pwillhan.

the class ReadOnly method immutableEntity.

@Test
public void immutableEntity() throws Exception {
    FetchTestData testData = storeTestData();
    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);
        for (Bid bid : item.getBids()) {
            // This has no effect
            bid.setAmount(new BigDecimal("99.99"));
        }
        em.flush();
        em.clear();
        item = em.find(Item.class, ITEM_ID);
        for (Bid bid : item.getBids()) {
            assertNotEquals(bid.getAmount().toString(), "99.99");
        }
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.fetching.readonly.Item) EntityManager(javax.persistence.EntityManager) Bid(org.jpwh.model.fetching.readonly.Bid) BigDecimal(java.math.BigDecimal) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 2 with Item

use of org.jpwh.model.fetching.readonly.Item in project microservices by pwillhan.

the class ReadOnly 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);
    em.persist(item);
    itemIds[0] = item.getId();
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid(item, robertdoe, new BigDecimal(9 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item Two", CalendarUtil.TOMORROW.getTime(), johndoe);
    em.persist(item);
    itemIds[1] = item.getId();
    for (int i = 1; i <= 1; i++) {
        Bid bid = new Bid(item, janeroe, 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.readonly.Item) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.fetching.readonly.User) TestData(org.jpwh.shared.util.TestData) Bid(org.jpwh.model.fetching.readonly.Bid) BigDecimal(java.math.BigDecimal)

Example 3 with Item

use of org.jpwh.model.fetching.readonly.Item in project microservices by pwillhan.

the class ReadOnly method selectiveReadOnly.

@Test
public void selectiveReadOnly() throws Exception {
    FetchTestData testData = storeTestData();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Long ITEM_ID = testData.items.getFirstId();
        {
            em.unwrap(Session.class).setDefaultReadOnly(true);
            Item item = em.find(Item.class, ITEM_ID);
            item.setName("New Name");
            // No UPDATE
            em.flush();
        }
        {
            em.clear();
            Item item = em.find(Item.class, ITEM_ID);
            assertNotEquals(item.getName(), "New Name");
        }
        {
            Item item = em.find(Item.class, ITEM_ID);
            em.unwrap(Session.class).setReadOnly(item, true);
            item.setName("New Name");
            // No UPDATE
            em.flush();
        }
        {
            em.clear();
            Item item = em.find(Item.class, ITEM_ID);
            assertNotEquals(item.getName(), "New Name");
        }
        {
            org.hibernate.Query query = em.unwrap(Session.class).createQuery("select i from Item i");
            query.setReadOnly(true).list();
            List<Item> result = query.list();
            for (Item item : result) item.setName("New Name");
            // No UPDATE
            em.flush();
        }
        {
            List<Item> items = em.createQuery("select i from Item i").setHint(org.hibernate.annotations.QueryHints.READ_ONLY, true).getResultList();
            for (Item item : items) item.setName("New Name");
            // No UPDATE
            em.flush();
        }
        {
            em.clear();
            Item item = em.find(Item.class, ITEM_ID);
            assertNotEquals(item.getName(), "New Name");
        }
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.fetching.readonly.Item) EntityManager(javax.persistence.EntityManager) List(java.util.List) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

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