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();
}
}
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();
}
}
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();
}
}
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();
}
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();
}
}
Aggregations