Search in sources :

Example 1 with CreditCard

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;
}
Also used : UserTransaction(javax.transaction.UserTransaction) User(org.jpwh.model.querying.User) Category(org.jpwh.model.querying.Category) Address(org.jpwh.model.querying.Address) TestData(org.jpwh.shared.util.TestData) BankAccount(org.jpwh.model.inheritance.tableperclass.BankAccount) Image(org.jpwh.model.querying.Image) BigDecimal(java.math.BigDecimal) CreditCard(org.jpwh.model.inheritance.tableperclass.CreditCard) Item(org.jpwh.model.querying.Item) EntityManager(javax.persistence.EntityManager) LogRecord(org.jpwh.model.querying.LogRecord) Bid(org.jpwh.model.querying.Bid)

Example 2 with CreditCard

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();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) UserTransaction(javax.transaction.UserTransaction) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) BankAccount(org.jpwh.model.inheritance.tableperclass.BankAccount) BillingDetails(org.jpwh.model.inheritance.tableperclass.BillingDetails) CreditCard(org.jpwh.model.inheritance.tableperclass.CreditCard) EntityType(javax.persistence.metamodel.EntityType) Item(org.jpwh.model.querying.Item) EntityManager(javax.persistence.EntityManager) List(java.util.List) Test(org.testng.annotations.Test) QueryingTest(org.jpwh.test.querying.QueryingTest)

Example 3 with CreditCard

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();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) CreditCard(org.jpwh.model.inheritance.tableperclass.CreditCard) Test(org.testng.annotations.Test) QueryingTest(org.jpwh.test.querying.QueryingTest)

Aggregations

EntityManager (javax.persistence.EntityManager)3 UserTransaction (javax.transaction.UserTransaction)3 CreditCard (org.jpwh.model.inheritance.tableperclass.CreditCard)3 Query (javax.persistence.Query)2 BankAccount (org.jpwh.model.inheritance.tableperclass.BankAccount)2 Item (org.jpwh.model.querying.Item)2 QueryingTest (org.jpwh.test.querying.QueryingTest)2 Test (org.testng.annotations.Test)2 BigDecimal (java.math.BigDecimal)1 List (java.util.List)1 TypedQuery (javax.persistence.TypedQuery)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Root (javax.persistence.criteria.Root)1 EntityType (javax.persistence.metamodel.EntityType)1 BillingDetails (org.jpwh.model.inheritance.tableperclass.BillingDetails)1 Address (org.jpwh.model.querying.Address)1 Bid (org.jpwh.model.querying.Bid)1 Category (org.jpwh.model.querying.Category)1 Image (org.jpwh.model.querying.Image)1