Search in sources :

Example 1 with BillingDetails

use of org.jpwh.model.inheritance.tableperclass.BillingDetails 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)

Aggregations

List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)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 UserTransaction (javax.transaction.UserTransaction)1 BankAccount (org.jpwh.model.inheritance.tableperclass.BankAccount)1 BillingDetails (org.jpwh.model.inheritance.tableperclass.BillingDetails)1 CreditCard (org.jpwh.model.inheritance.tableperclass.CreditCard)1 Item (org.jpwh.model.querying.Item)1 QueryingTest (org.jpwh.test.querying.QueryingTest)1 Test (org.testng.annotations.Test)1