use of org.jpwh.model.querying.LogRecord 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.LogRecord in project microservices by pwillhan.
the class Joins 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();
{
// Implicit inner join
CriteriaQuery criteria = cb.createQuery();
Root<Bid> b = criteria.from(Bid.class);
criteria.select(b).where(cb.like(b.get("item").<String>get("name"), "Fo%"));
Query q = em.createQuery(criteria);
List<Bid> result = q.getResultList();
assertEquals(result.size(), 3);
for (Bid bid : result) {
assertEquals(bid.getItem().getId(), testData.items.getFirstId());
}
}
em.clear();
{
// Multiple inner
CriteriaQuery<Bid> criteria = cb.createQuery(Bid.class);
Root<Bid> b = criteria.from(Bid.class);
criteria.select(b).where(cb.equal(b.get("item").get("seller").get("username"), "johndoe"));
Query q = em.createQuery(criteria);
List<Bid> result = q.getResultList();
assertEquals(result.size(), 4);
}
em.clear();
{
// Multiple inner
CriteriaQuery criteria = cb.createQuery();
Root<Bid> b = criteria.from(Bid.class);
criteria.select(b).where(cb.and(cb.equal(b.get("item").get("seller").get("username"), "johndoe"), cb.isNotNull(b.get("item").get("buyNowPrice"))));
Query q = em.createQuery(criteria);
List<Bid> result = q.getResultList();
assertEquals(result.size(), 3);
}
em.clear();
{
// Explicit inner
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
Join<Item, Bid> b = i.join("bids");
criteria.select(i).where(cb.gt(b.<BigDecimal>get("amount"), new BigDecimal(100)));
Query q = em.createQuery(criteria);
List<Item> result = q.getResultList();
assertEquals(result.size(), 1);
assertEquals(result.get(0).getId(), testData.items.getFirstId());
}
em.clear();
{
// Explicit outer
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
Join<Item, Bid> b = i.join("bids", JoinType.LEFT);
b.on(cb.gt(b.<BigDecimal>get("amount"), new BigDecimal(100)));
criteria.multiselect(i, b);
Query q = em.createQuery(criteria);
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 3);
assertTrue(result.get(0)[0] instanceof Item);
assertTrue(result.get(0)[1] instanceof Bid);
assertTrue(result.get(1)[0] instanceof Item);
assertEquals(result.get(1)[1], null);
assertTrue(result.get(2)[0] instanceof Item);
assertEquals(result.get(2)[1], null);
}
em.clear();
{
// Explicit right outer
/* TODO Right outer joins not supported in criteria, Hibernate bug JPA-2
CriteriaQuery criteria = cb.createQuery();
Root<Bid> b = criteria.from(Bid.class);
Join<Bid, Item> i = b.join("item", JoinType.RIGHT);
criteria.multiselect(b, i).where(
cb.or(
cb.isNull(b),
cb.gt(b.<BigDecimal>get("amount"), new BigDecimal(100)))
);
Query q = em.createQuery(criteria);
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 2);
assertTrue(result.get(0)[0] instanceof Bid);
assertTrue(result.get(0)[1] instanceof Item);
assertEquals(result.get(1)[0], null);
assertTrue(result.get(1)[1] instanceof Item);
*/
}
em.clear();
{
// A typical "fetch the bids collections for all items as a side effect" query
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
i.fetch("bids", JoinType.LEFT);
criteria.select(i);
Query q = em.createQuery(criteria);
List<Item> result = q.getResultList();
// 3 items, 4 bids, 5 "rows" in result!
assertEquals(result.size(), 5);
// In-memory "distinct"
Set<Item> distinctResult = new LinkedHashSet<Item>(result);
// It was only three items all along...
assertEquals(distinctResult.size(), 3);
boolean haveBids = false;
for (Item item : distinctResult) {
// No more lazy loading!
em.detach(item);
if (item.getBids().size() > 0) {
haveBids = true;
break;
}
}
assertTrue(haveBids);
}
em.clear();
{
// Hibernate can remove the duplicate SQL resultset rows for you
// (...who knows, some of you actually WANT the duplicates!)
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
i.fetch("bids", JoinType.LEFT);
criteria.select(i).distinct(true);
Query q = em.createQuery(criteria);
List<Item> result = q.getResultList();
// Hibernate filtered it for us in-memory
assertEquals(result.size(), 3);
boolean haveBids = false;
for (Item item : result) {
// No more lazy loading!
em.detach(item);
if (item.getBids().size() > 0) {
haveBids = true;
break;
}
}
assertTrue(haveBids);
}
em.clear();
{
// Fetch multiple associated instances/collections
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
Fetch<Item, Bid> b = i.fetch("bids", JoinType.LEFT);
// These are non-nullable foreign key columns, inner join or
b.fetch("bidder");
// outer doesn't make a difference!
i.fetch("seller", JoinType.LEFT);
criteria.select(i).distinct(true);
Query q = em.createQuery(criteria);
List<Item> result = q.getResultList();
assertEquals(result.size(), 2);
boolean haveBids = false;
boolean haveBidder = false;
boolean haveSeller = false;
for (Item item : result) {
// No more lazy loading!
em.detach(item);
if (item.getBids().size() > 0) {
haveBids = true;
Bid bid = item.getBids().iterator().next();
if (bid.getBidder() != null && bid.getBidder().getUsername() != null) {
haveBidder = true;
}
}
if (item.getSeller() != null && item.getSeller().getUsername() != null)
haveSeller = true;
}
assertTrue(haveBids);
assertTrue(haveBidder);
assertTrue(haveSeller);
}
em.clear();
{
// SQL Cartesian product of multiple collections! Bad!
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
i.fetch("bids", JoinType.LEFT);
// Cartesian product, bad!
i.fetch("images", JoinType.LEFT);
criteria.select(i).distinct(true);
Query q = em.createQuery(criteria);
List<Item> result = q.getResultList();
assertEquals(result.size(), 3);
boolean haveBids = false;
boolean haveImages = false;
for (Item item : result) {
// No more lazy loading!
em.detach(item);
if (item.getBids().size() > 0)
haveBids = true;
if (item.getImages().size() > 0)
haveImages = true;
}
assertTrue(haveBids);
assertTrue(haveImages);
}
em.clear();
{
// Theta style inner join
CriteriaQuery criteria = cb.createQuery();
Root<User> u = criteria.from(User.class);
Root<LogRecord> log = criteria.from(LogRecord.class);
criteria.where(cb.equal(u.get("username"), log.get("username")));
criteria.multiselect(u, log);
Query q = em.createQuery(criteria);
List<Object[]> result = q.getResultList();
for (Object[] row : result) {
assertTrue(row[0] instanceof User);
assertTrue(row[1] instanceof LogRecord);
}
assertEquals(result.size(), 2);
}
em.clear();
{
// Theta style inner join, multiple
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
Root<Bid> b = criteria.from(Bid.class);
criteria.where(cb.equal(b.get("item"), i), cb.equal(i.get("seller"), b.get("bidder")));
criteria.multiselect(i, b);
Query q = em.createQuery(criteria);
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 0);
}
em.clear();
{
// Inner join with implicit ID comparison
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
Root<User> u = criteria.from(User.class);
criteria.where(cb.equal(i.get("seller"), u), cb.like(u.<String>get("username"), "j%"));
criteria.multiselect(i, u);
Query q = em.createQuery(criteria);
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 3);
for (Object[] row : result) {
assertTrue(row[0] instanceof Item);
assertTrue(row[1] instanceof User);
}
}
em.clear();
{
// Inner join with explicit ID comparison
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
Root<User> u = criteria.from(User.class);
criteria.where(cb.equal(i.get("seller").get("id"), u.get("id")), cb.like(u.<String>get("username"), "j%"));
criteria.multiselect(i, u);
Query q = em.createQuery(criteria);
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 3);
for (Object[] row : result) {
assertTrue(row[0] instanceof Item);
assertTrue(row[1] instanceof User);
}
}
em.clear();
{
// Binding an entity parameter
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.where(cb.equal(i.get("seller"), cb.parameter(User.class, "seller")));
criteria.select(i);
Query q = em.createQuery(criteria);
User someUser = em.find(User.class, testData.users.getFirstId());
q.setParameter("seller", someUser);
List<Item> result = q.getResultList();
assertEquals(result.size(), 2);
}
em.clear();
{
// Binding ID parameter
CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.where(cb.equal(i.get("seller").get("id"), cb.parameter(Long.class, "sellerId")));
criteria.select(i);
Query q = em.createQuery(criteria);
Long USER_ID = testData.users.getFirstId();
q.setParameter("sellerId", USER_ID);
List<Item> result = q.getResultList();
assertEquals(result.size(), 2);
}
em.clear();
{
// Not a join, just ID comparison
CriteriaQuery<Bid> criteria = cb.createQuery(Bid.class);
Root<Bid> b = criteria.from(Bid.class);
criteria.where(cb.equal(b.get("item").get("id"), cb.parameter(Long.class, "itemId")));
criteria.select(b);
Query q = em.createQuery(criteria);
q.setParameter("itemId", testData.items.getFirstId());
List<Bid> result = q.getResultList();
assertEquals(result.size(), 3);
}
em.clear();
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of org.jpwh.model.querying.LogRecord in project microservices by pwillhan.
the class Joins method executeQueries.
@Test
public void executeQueries() throws Exception {
TestDataCategoriesItems testData = storeTestData();
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
{
Query q = em.createNamedQuery("implicitInner");
List<Bid> result = q.getResultList();
assertEquals(result.size(), 3);
for (Bid bid : result) {
assertEquals(bid.getItem().getId(), testData.items.getFirstId());
}
}
em.clear();
{
Query q = em.createNamedQuery("multipleImplicitInner");
List<Bid> result = q.getResultList();
assertEquals(result.size(), 4);
}
em.clear();
{
Query q = em.createNamedQuery("multipleImplicitInnerAnd");
List<Bid> result = q.getResultList();
assertEquals(result.size(), 3);
}
em.clear();
{
Query q = em.createNamedQuery("explicitInner");
List<Item> result = q.getResultList();
assertEquals(result.size(), 1);
assertEquals(result.get(0).getId(), testData.items.getFirstId());
}
em.clear();
{
Query q = em.createNamedQuery("explicitOuter");
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 3);
assertTrue(result.get(0)[0] instanceof Item);
assertTrue(result.get(0)[1] instanceof Bid);
assertTrue(result.get(1)[0] instanceof Item);
assertEquals(result.get(1)[1], null);
assertTrue(result.get(2)[0] instanceof Item);
assertEquals(result.get(2)[1], null);
}
em.clear();
{
Query q = em.createNamedQuery("explicitOuterRight");
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 2);
assertTrue(result.get(0)[0] instanceof Bid);
assertTrue(result.get(0)[1] instanceof Item);
assertEquals(result.get(1)[0], null);
assertTrue(result.get(1)[1] instanceof Item);
}
em.clear();
{
Query query = em.createNamedQuery("outerFetchCollection");
List<Item> result = query.getResultList();
// 3 items, 4 bids, 5 "rows" in result!
assertEquals(result.size(), 5);
// In-memory "distinct"
Set<Item> distinctResult = new LinkedHashSet<Item>(result);
// It was only three items all along...
assertEquals(distinctResult.size(), 3);
boolean haveBids = false;
for (Item item : distinctResult) {
// No more lazy loading!
em.detach(item);
if (item.getBids().size() > 0) {
haveBids = true;
break;
}
}
assertTrue(haveBids);
}
em.clear();
{
Query q = em.createNamedQuery("outerFetchCollectionDistinct");
List<Item> result = q.getResultList();
// Hibernate filtered it for us in-memory
assertEquals(result.size(), 3);
boolean haveBids = false;
for (Item item : result) {
// No more lazy loading!
em.detach(item);
if (item.getBids().size() > 0) {
haveBids = true;
break;
}
}
assertTrue(haveBids);
}
em.clear();
{
Query q = em.createNamedQuery("outerFetchMultiple");
List<Item> result = q.getResultList();
assertEquals(result.size(), 2);
boolean haveBids = false;
boolean haveBidder = false;
boolean haveSeller = false;
for (Item item : result) {
// No more lazy loading!
em.detach(item);
if (item.getBids().size() > 0) {
haveBids = true;
Bid bid = item.getBids().iterator().next();
if (bid.getBidder() != null && bid.getBidder().getUsername() != null) {
haveBidder = true;
}
}
if (item.getSeller() != null && item.getSeller().getUsername() != null)
haveSeller = true;
}
assertTrue(haveBids);
assertTrue(haveBidder);
assertTrue(haveSeller);
}
em.clear();
{
// SQL Cartesian product of multiple collections! Bad!
Query q = em.createNamedQuery("badProductFetch");
List<Item> result = q.getResultList();
assertEquals(result.size(), 3);
boolean haveBids = false;
boolean haveImages = false;
for (Item item : result) {
// No more lazy loading!
em.detach(item);
if (item.getBids().size() > 0)
haveBids = true;
if (item.getImages().size() > 0)
haveImages = true;
}
assertTrue(haveBids);
assertTrue(haveImages);
}
em.clear();
{
Query query = em.createNamedQuery("thetaStyle");
List<Object[]> result = query.getResultList();
for (Object[] row : result) {
assertTrue(row[0] instanceof User);
assertTrue(row[1] instanceof LogRecord);
}
assertEquals(result.size(), 2);
}
em.clear();
{
Query q = em.createNamedQuery("thetaEqualsId");
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 0);
}
em.clear();
{
Query q = em.createNamedQuery("implicitEqualsId");
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 3);
for (Object[] row : result) {
assertTrue(row[0] instanceof Item);
assertTrue(row[1] instanceof User);
}
}
em.clear();
{
Query q = em.createNamedQuery("explicitEqualsId");
List<Object[]> result = q.getResultList();
assertEquals(result.size(), 3);
for (Object[] row : result) {
assertTrue(row[0] instanceof Item);
assertTrue(row[1] instanceof User);
}
}
em.clear();
{
User someUser = em.find(User.class, testData.users.getFirstId());
Query query = em.createNamedQuery("parameterEqualsEntity");
query.setParameter("seller", someUser);
List<Item> result = query.getResultList();
assertEquals(result.size(), 2);
}
em.clear();
{
Long USER_ID = testData.users.getFirstId();
Query query = em.createNamedQuery("parameterEqualsId");
query.setParameter("sellerId", USER_ID);
List<Item> result = query.getResultList();
assertEquals(result.size(), 2);
}
em.clear();
{
Query q = em.createNamedQuery("nojoinEqualsId");
q.setParameter("itemId", testData.items.getFirstId());
List<Bid> result = q.getResultList();
assertEquals(result.size(), 3);
}
em.clear();
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
Aggregations