Search in sources :

Example 1 with ItemBidSummary

use of org.jpwh.model.advanced.ItemBidSummary 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)

Aggregations

EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 UserTransaction (javax.transaction.UserTransaction)1 JPATest (org.jpwh.env.JPATest)1 Item (org.jpwh.model.advanced.Item)1 ItemBidSummary (org.jpwh.model.advanced.ItemBidSummary)1 Test (org.testng.annotations.Test)1