use of org.jpwh.model.inheritance.tableperclass.CreditCard in project microservices by pwillhan.
the class QueryingTest method storeTestData.
public TestDataCategoriesItems storeTestData() throws Exception {
UserTransaction tx = TM.getUserTransaction();
tx.begin();
EntityManager em = JPA.createEntityManager();
Long[] categoryIds = new Long[4];
Long[] itemIds = new Long[3];
Long[] userIds = new Long[3];
User johndoe = new User("johndoe", "John", "Doe");
Address homeAddress = new Address("Some Street 123", "12345", "Some City");
johndoe.setActivated(true);
johndoe.setHomeAddress(homeAddress);
em.persist(johndoe);
userIds[0] = johndoe.getId();
User janeroe = new User("janeroe", "Jane", "Roe");
janeroe.setActivated(true);
janeroe.setHomeAddress(new Address("Other Street 11", "1234", "Other City"));
em.persist(janeroe);
userIds[1] = janeroe.getId();
User robertdoe = new User("robertdoe", "Robert", "Doe");
em.persist(robertdoe);
userIds[2] = robertdoe.getId();
Category categoryOne = new Category("One");
em.persist(categoryOne);
categoryIds[0] = categoryOne.getId();
Item item = new Item("Foo", CalendarUtil.TOMORROW.getTime(), johndoe);
item.setBuyNowPrice(new BigDecimal("19.99"));
em.persist(item);
itemIds[0] = item.getId();
categoryOne.getItems().add(item);
item.getCategories().add(categoryOne);
for (int i = 1; i <= 3; i++) {
Bid bid = new Bid(item, robertdoe, new BigDecimal(98 + i));
item.getBids().add(bid);
em.persist(bid);
}
item.getImages().add(new Image("Foo", "foo.jpg", 640, 480));
item.getImages().add(new Image("Bar", "bar.jpg", 800, 600));
item.getImages().add(new Image("Baz", "baz.jpg", 1024, 768));
Category categoryTwo = new Category("Two");
categoryTwo.setParent(categoryOne);
em.persist(categoryTwo);
categoryIds[1] = categoryTwo.getId();
item = new Item("Bar", CalendarUtil.TOMORROW.getTime(), johndoe);
em.persist(item);
itemIds[1] = item.getId();
categoryTwo.getItems().add(item);
item.getCategories().add(categoryTwo);
Bid bid = new Bid(item, janeroe, new BigDecimal("4.99"));
item.getBids().add(bid);
em.persist(bid);
item = new Item("Baz", CalendarUtil.AFTER_TOMORROW.getTime(), janeroe);
item.setApproved(false);
em.persist(item);
itemIds[2] = item.getId();
categoryTwo.getItems().add(item);
item.getCategories().add(categoryTwo);
Category categoryThree = new Category("Three");
categoryThree.setParent(categoryOne);
em.persist(categoryThree);
categoryIds[2] = categoryThree.getId();
Category categoryFour = new Category("Four");
categoryFour.setParent(categoryTwo);
em.persist(categoryFour);
categoryIds[3] = categoryFour.getId();
CreditCard cc = new CreditCard("John Doe", "1234123412341234", "06", "2015");
em.persist(cc);
BankAccount ba = new BankAccount("Jane Roe", "445566", "One Percent Bank Inc.", "999");
em.persist(ba);
LogRecord lr = new LogRecord("johndoe", "This is a log message");
em.persist(lr);
lr = new LogRecord("johndoe", "Another log message");
em.persist(lr);
tx.commit();
em.close();
TestDataCategoriesItems testData = new TestDataCategoriesItems();
testData.categories = new TestData(categoryIds);
testData.items = new TestData(itemIds);
testData.users = new TestData(userIds);
return testData;
}
use of org.jpwh.model.inheritance.tableperclass.CreditCard in project microservices by pwillhan.
the class Selection method executeQueries.
@Test
public void executeQueries() throws Exception {
storeTestData();
CriteriaBuilder cb = JPA.getEntityManagerFactory().getCriteriaBuilder();
// or:
// CriteriaBuilder cb = em.getCriteriaBuilder();
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
{
// This is not guaranteed to work in all JPA providers, criteria.select() should be used
CriteriaQuery criteria = cb.createQuery(Item.class);
criteria.from(Item.class);
List<Item> result = em.createQuery(criteria).getResultList();
assertEquals(result.size(), 3);
}
em.clear();
{
// Simplest
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i);
List<Item> result = em.createQuery(criteria).getResultList();
assertEquals(result.size(), 3);
}
em.clear();
{
// Nested calls
CriteriaQuery criteria = cb.createQuery();
criteria.select(criteria.from(Item.class));
TypedQuery<Item> q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
// Restrict type with metamodel
CriteriaQuery criteria = cb.createQuery();
EntityType entityType = getEntityType(em.getMetamodel(), "Item");
criteria.select(criteria.from(entityType));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
// Polymorphism restricted types
CriteriaQuery criteria = cb.createQuery();
criteria.select(criteria.from(BillingDetails.class));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
CriteriaQuery criteria = cb.createQuery();
criteria.select(criteria.from(CreditCard.class));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
// Polymorphism restricted types
CriteriaQuery criteria = cb.createQuery();
Root<BillingDetails> bd = criteria.from(BillingDetails.class);
criteria.select(bd).where(cb.equal(bd.type(), CreditCard.class));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
// Polymorphism restricted types
CriteriaQuery criteria = cb.createQuery();
Root<BillingDetails> bd = criteria.from(BillingDetails.class);
criteria.select(bd).where(cb.not(cb.equal(bd.type(), BankAccount.class)));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
assertTrue(q.getResultList().iterator().next() instanceof CreditCard);
}
em.clear();
{
// Polymorphism restricted types
CriteriaQuery criteria = cb.createQuery();
Root<BillingDetails> bd = criteria.from(BillingDetails.class);
criteria.select(bd).where(bd.type().in(cb.parameter(List.class, "types")));
Query q = em.createQuery(criteria);
q.setParameter("types", Arrays.asList(CreditCard.class, BankAccount.class));
assertEquals(q.getResultList().size(), 2);
}
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of org.jpwh.model.inheritance.tableperclass.CreditCard in project microservices by pwillhan.
the class Selection method executeQueries.
@Test
public void executeQueries() throws Exception {
TestDataCategoriesItems testData = storeTestData();
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
{
// This only works in Hibernate, SELECT clause isn't optional in JPA
Query q = em.createNamedQuery("selection");
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
Query q = em.createNamedQuery("aliases");
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
Query q = em.createNamedQuery("aliasesAs");
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
Query q = em.createNamedQuery("polymorphismObject");
assertEquals(q.getResultList().size(), 18);
}
em.clear();
{
Query q = em.createNamedQuery("polymorphismInterface");
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
Query q = em.createNamedQuery("polymorphismBillingDetails");
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
Query q = em.createNamedQuery("polymorphismCreditCard");
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
Query q = em.createNamedQuery("polymorphismRestrictName");
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
// ...
Query query = em.createNamedQuery("polymorphismRestrictParameter");
query.setParameter("types", Arrays.asList(CreditCard.class, BankAccount.class));
assertEquals(query.getResultList().size(), 2);
}
em.clear();
{
Query q = em.createNamedQuery("polymorphismRestrictNot");
assertEquals(q.getResultList().size(), 1);
assertTrue(q.getResultList().iterator().next() instanceof CreditCard);
}
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
Aggregations