use of org.jpwh.model.querying.Category 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.querying.Category in project microservices by pwillhan.
the class HibernateSQLQueries method executeRecursiveQueries.
@Test(groups = { "H2" })
public void executeRecursiveQueries() throws Exception {
TestDataCategoriesItems testData = storeTestData();
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
Session session = em.unwrap(Session.class);
{
// Externalized recursive SQL query
org.hibernate.Query query = session.getNamedQuery("findAllCategoriesHibernate");
List<Object[]> result = query.list();
for (Object[] tuple : result) {
Category category = (Category) tuple[0];
String path = (String) tuple[1];
Integer level = (Integer) tuple[2];
// ...
/*
System.out.println("------------------------------------------------");
System.out.println("### PATH: " + path);
System.out.println("### NAME: " + category.getName());
System.out.println("### PARENT: " + (category.getParent() != null ? category.getParent().getName() : "ROOT"));
System.out.println("### LEVEL " + level);
*/
}
}
em.clear();
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of org.jpwh.model.querying.Category in project microservices by pwillhan.
the class Restriction method executeQueries.
@Test
public void executeQueries() throws Exception {
TestDataCategoriesItems testData = storeTestData();
CriteriaBuilder cb = JPA.getEntityManagerFactory().getCriteriaBuilder();
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
{
// select i from Item i where i.name = 'Foo'
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(cb.equal(i.get("name"), "Foo"));
TypedQuery<Item> q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
assertEquals(q.getResultList().iterator().next().getName(), "Foo");
}
em.clear();
{
// Equals boolean
CriteriaQuery criteria = cb.createQuery();
Root<User> u = criteria.from(User.class);
criteria.select(u).where(cb.equal(u.get("activated"), true));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
// Between
CriteriaQuery criteria = cb.createQuery();
Root<Bid> b = criteria.from(Bid.class);
criteria.select(b).where(cb.between(// Type of path required!
b.<BigDecimal>get("amount"), // Must be same type!
new BigDecimal("99"), // Must be same type!
new BigDecimal("110")));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
// Greater than
CriteriaQuery criteria = cb.createQuery();
Root<Bid> b = criteria.from(Bid.class);
criteria.select(b).where(// gt() only works with Number, use greaterThan() otherwise!
cb.gt(b.<BigDecimal>get("amount"), new BigDecimal("100")));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
// Greater than with date (!Number)
Date tomorrowDate = CalendarUtil.TOMORROW.getTime();
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(cb.greaterThan(i.<Date>get("auctionEnd"), tomorrowDate));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
// IN list
CriteriaQuery criteria = cb.createQuery();
Root<User> u = criteria.from(User.class);
criteria.select(u).where(cb.<String>in(u.<String>get("username")).value("johndoe").value("janeroe"));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
// Enum
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(cb.equal(i.<AuctionType>get("auctionType"), AuctionType.HIGHEST_BID));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
// Ternary operators
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(cb.isNull(i.get("buyNowPrice")));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(cb.isNotNull(i.get("buyNowPrice")));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
// String matching
CriteriaQuery criteria = cb.createQuery();
Root<User> u = criteria.from(User.class);
criteria.select(u).where(cb.like(u.<String>get("username"), "john%"));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
}
{
// String matching
CriteriaQuery criteria = cb.createQuery();
Root<User> u = criteria.from(User.class);
criteria.select(u).where(cb.like(u.<String>get("username"), "john%").not());
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
CriteriaQuery criteria = cb.createQuery();
Root<User> u = criteria.from(User.class);
criteria.select(u).where(cb.like(u.<String>get("username"), "%oe%"));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(cb.like(i.<String>get("name"), "Name\\_with\\_underscores", '\\'));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 0);
}
em.clear();
{
// Arithmetic
CriteriaQuery criteria = cb.createQuery();
Root<Bid> b = criteria.from(Bid.class);
criteria.select(b).where(cb.gt(cb.diff(cb.quot(b.<BigDecimal>get("amount"), 2), 0.5), 49));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
// Logical groups
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
Predicate predicate = cb.and(cb.like(i.<String>get("name"), "Fo%"), cb.isNotNull(i.get("buyNowPrice")));
predicate = cb.or(predicate, cb.equal(i.<String>get("name"), "Bar"));
criteria.select(i).where(predicate);
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(cb.like(i.<String>get("name"), "Fo%"), // AND
cb.isNotNull(i.get("buyNowPrice")));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
// Collection functions
CriteriaQuery criteria = cb.createQuery();
Root<Category> c = criteria.from(Category.class);
criteria.select(c).where(cb.isNotEmpty(c.<Collection>get("items")));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
// Collection functions
CriteriaQuery criteria = cb.createQuery();
Root<Category> c = criteria.from(Category.class);
criteria.select(c).where(cb.isMember(cb.parameter(Item.class, "item"), c.<Collection<Item>>get("items")));
Query q = em.createQuery(criteria);
Item item = em.find(Item.class, testData.items.getFirstId());
q.setParameter("item", item);
List<Category> result = q.getResultList();
assertEquals(result.size(), 1);
}
em.clear();
{
// Collection functions
CriteriaQuery criteria = cb.createQuery();
Root<Category> c = criteria.from(Category.class);
criteria.select(c).where(cb.gt(cb.size(c.<Collection>get("items")), 1));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
}
em.clear();
{
// Calling functions
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(cb.like(cb.lower(i.<String>get("name")), "ba%"));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 2);
}
em.clear();
{
// Ordering result
CriteriaQuery criteria = cb.createQuery();
Root<User> u = criteria.from(User.class);
criteria.select(u).orderBy(cb.desc(u.get("username")));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 3);
}
em.clear();
{
// Ordering result
CriteriaQuery criteria = cb.createQuery();
Root<User> u = criteria.from(User.class);
criteria.select(u).orderBy(cb.desc(u.get("activated")), cb.asc(u.get("username")));
Query q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 3);
}
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of org.jpwh.model.querying.Category in project microservices by pwillhan.
the class NativeQueries method executeRecursiveQueries.
@Test(groups = { "H2" })
public void executeRecursiveQueries() throws Exception {
TestDataCategoriesItems testData = storeTestData();
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
{
// Externalized recursive SQL query
Query query = em.createNamedQuery("findAllCategories");
List<Object[]> result = query.getResultList();
for (Object[] tuple : result) {
Category category = (Category) tuple[0];
String path = (String) tuple[1];
Integer level = (Integer) tuple[2];
// ...
}
/*
System.out.println("------------------------------------------------");
System.out.println("### PATH: " + path);
System.out.println("### NAME: " + category.getName());
System.out.println("### PARENT: " + (category.getParent() != null ? category.getParent().getName() : "ROOT"));
System.out.println("### LEVEL " + level);
*/
}
em.clear();
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
Aggregations