Search in sources :

Example 1 with Item

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

the class GeneratedProperties method storeLoadLastModified.

@Test(groups = { "H2" })
public void storeLoadLastModified() throws Exception {
    long ITEM_ID = storeItemAndBids();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        assertNotNull(item.getCreatedOn());
        Date lastModified = item.getLastModified();
        assertNotNull(lastModified);
        assertTrue(item.getCreatedOn().getTime() < lastModified.getTime());
        item.setDescription("Some modification.");
        em.flush();
        Date newLastModified = item.getLastModified();
        assertNotEquals(lastModified, newLastModified, "Modification time should have been updated");
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Date(java.util.Date) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 2 with Item

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

the class LazyProperties method storeLoadLocator.

@Test
public void storeLoadLocator() throws Exception {
    // TODO: This test fails on H2 standalone
    // http://groups.google.com/group/h2-database/browse_thread/thread/9c6f4893a62c9b1a
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        byte[] bytes = new byte[131072];
        new Random().nextBytes(bytes);
        InputStream imageInputStream = new ByteArrayInputStream(bytes);
        int byteLength = bytes.length;
        Item someItem = new Item();
        someItem.setName("Some item");
        someItem.setDescription("This is some description.");
        // Need the native Hibernate API
        Session session = em.unwrap(Session.class);
        // You need to know the number of bytes you want to read from the stream!
        Blob blob = session.getLobHelper().createBlob(imageInputStream, byteLength);
        someItem.setImageBlob(blob);
        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);
        // You can stream the bytes directly...
        InputStream imageDataStream = item.getImageBlob().getBinaryStream();
        // ... or materialize them into memory:
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        StreamUtils.copy(imageDataStream, outStream);
        byte[] imageBytes = outStream.toByteArray();
        assertEquals(imageBytes.length, 131072);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Blob(java.sql.Blob) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Session(org.hibernate.Session) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 3 with Item

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

the class LazyProperties method storeLoadProperties.

@Test
public void storeLoadProperties() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.setName("Some item");
        someItem.setDescription("This is some description.");
        byte[] bytes = new byte[131072];
        new Random().nextBytes(bytes);
        someItem.setImage(bytes);
        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);
        // Accessing one initializes ALL lazy properties in a single SELECT
        assertEquals(item.getDescription(), "This is some description.");
        // 128 kilobytes
        assertEquals(item.getImage().length, 131072);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Random(java.util.Random) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 4 with Item

use of org.jpwh.model.advanced.Item 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 5 with Item

use of org.jpwh.model.advanced.Item 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)16 UserTransaction (javax.transaction.UserTransaction)16 Item (org.jpwh.model.advanced.Item)16 JPATest (org.jpwh.env.JPATest)12 Test (org.testng.annotations.Test)12 BigDecimal (java.math.BigDecimal)6 Bid (org.jpwh.model.advanced.Bid)4 Date (java.util.Date)2 Random (java.util.Random)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Blob (java.sql.Blob)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 Query (javax.persistence.Query)1