use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class SetOfStrings method storeLoadCollection.
@Test
public void storeLoadCollection() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
Item someItem = new Item();
someItem.getImages().add("foo.jpg");
someItem.getImages().add("bar.jpg");
someItem.getImages().add("baz.jpg");
// Duplicate, filtered at Java level by HashSet!
someItem.getImages().add("baz.jpg");
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);
assertEquals(item.getImages().size(), 3);
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class SortedSetOfStrings method storeLoadCollection.
@Test
public void storeLoadCollection() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
Item someItem = new Item();
someItem.getImages().add("foo.jpg");
someItem.getImages().add("bar.jpg");
someItem.getImages().add("baz.jpg");
// Duplicate, filtered at Java level by HashSet!
someItem.getImages().add("baz.jpg");
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);
assertEquals(item.getImages().size(), 3);
// Sorted in-memory with TreeSet
Iterator<String> it = item.getImages().iterator();
String image;
image = it.next();
assertEquals(image, "bar.jpg");
image = it.next();
assertEquals(image, "baz.jpg");
image = it.next();
assertEquals(image, "foo.jpg");
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class CompositeKeyManyToOne method storeLoad.
@Test
public void storeLoad() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
{
UserId id = new UserId("johndoe", "123");
User user = new User(id);
em.persist(user);
Item item = new Item("Some Item");
item.setSeller(user);
em.persist(item);
}
tx.commit();
em.close();
tx.begin();
em = JPA.createEntityManager();
{
UserId id = new UserId("johndoe", "123");
User user = em.find(User.class, id);
assertEquals(user.getId().getDepartmentNr(), "123");
Item item = (Item) em.createQuery("select i from Item i where i.seller = :u").setParameter("u", user).getSingleResult();
assertEquals(item.getName(), "Some Item");
}
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class CustomSchema method storeLoadDomainInvalid.
// All these tests are testing for failure
@Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
public void storeLoadDomainInvalid() throws Throwable {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
// This will fail and therefore validate that we actually
// have a custom SQL datatype in the DDL
User user = new User();
user.setEmail("@invalid.address");
user.setUsername("someuser");
em.persist(user);
try {
em.flush();
} catch (Exception ex) {
throw unwrapCauseOfType(ex, org.hibernate.exception.ConstraintViolationException.class);
}
} finally {
TM.rollback();
}
}
use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class CustomSchema method storeLoadCheckColumnInvalid.
@Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
public void storeLoadCheckColumnInvalid() throws Throwable {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
User user = new User();
user.setEmail("valid@test.com");
user.setUsername("adminPretender");
em.persist(user);
try {
em.flush();
} catch (Exception ex) {
throw unwrapCauseOfType(ex, org.hibernate.exception.ConstraintViolationException.class);
}
} finally {
TM.rollback();
}
}
Aggregations