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