Search in sources :

Example 6 with Employee

use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.

the class JPQLQueryTest method testMemberOfViaUnboundVariableJoinTable.

/**
 * Test of JPQL "MEMBER [OF] (container-expr)" syntax, using JoinTable.
 */
public void testMemberOfViaUnboundVariableJoinTable() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Employee e1 = new Employee(101, "Fred", "Flintstone", "fred.flintstone@jpox.com", 30000f, "1234A");
            Employee e2 = new Employee(102, "Barney", "Rubble", "barney.rubble@jpox.com", 27000f, "1234B");
            Employee e3 = new Employee(103, "George", "Cement", "george.cement@jpox.com", 20000f, "1235C");
            Manager mgr1 = new Manager(100, "Chief", "Rock", "chief.rock@warnerbros.com", 40000.0f, "12345A");
            mgr1.setBestFriend(e1);
            Manager mgr2 = new Manager(106, "Boss", "Blaster", "boss.blaster@warnerbros.com", 40005.0f, "12345B");
            mgr2.setBestFriend(e2);
            mgr1.addSubordinate(e1);
            mgr1.addSubordinate(e2);
            e1.setManager(mgr1);
            e2.setManager(mgr1);
            mgr2.addSubordinate(e3);
            e3.setManager(mgr2);
            em.persist(mgr1);
            em.persist(mgr2);
            em.flush();
            List result = em.createQuery("SELECT DISTINCT m FROM " + Manager.class.getName() + " m," + Employee.class.getName() + " e " + "WHERE e MEMBER OF m.subordinates AND e.firstName = 'Barney'").getResultList();
            // Manager 1
            assertEquals(1, result.size());
            Manager mgr = (Manager) result.get(0);
            assertEquals("Manager returned from MEMBER OF query has incorrect firstName", "Chief", mgr.getFirstName());
            assertEquals("Manager returned from MEMBER OF query has incorrect lastName", "Rock", mgr.getLastName());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown generating query with MEMBER syntax " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        removePersonBestFriendRelation();
        removeManagerEmployeeRelation();
        clean(Manager.class);
        clean(Employee.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) List(java.util.List) ArrayList(java.util.ArrayList) StoreManager(org.datanucleus.store.StoreManager) Manager(org.datanucleus.samples.annotations.models.company.Manager) EntityManager(javax.persistence.EntityManager) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException) PersistenceException(javax.persistence.PersistenceException)

Example 7 with Employee

use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.

the class JPQLQueryTest method testMemberOf.

/**
 * Test of JPQL "MEMBER [OF] (container-expr)" syntax.
 */
public void testMemberOf() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Employee e1 = new Employee(101, "Fred", "Flintstone", "fred.flintstone@jpox.com", 30000f, "1234A");
            Employee e2 = new Employee(102, "Barney", "Rubble", "barney.rubble@jpox.com", 27000f, "1234B");
            Employee e3 = new Employee(103, "George", "Cement", "george.cement@jpox.com", 20000f, "1235C");
            Manager mgr1 = new Manager(100, "Chief", "Rock", "chief.rock@warnerbros.com", 40000.0f, "12345A");
            mgr1.setBestFriend(e1);
            Manager mgr2 = new Manager(106, "Boss", "Blaster", "boss.blaster@warnerbros.com", 40005.0f, "12345B");
            mgr2.setBestFriend(e2);
            mgr1.addSubordinate(e1);
            mgr1.addSubordinate(e2);
            e1.setManager(mgr1);
            e2.setManager(mgr1);
            mgr2.addSubordinate(e3);
            e3.setManager(mgr2);
            em.persist(mgr1);
            em.persist(mgr2);
            em.flush();
            // Retrieve an Employee to use in MEMBER of queries
            Employee emp = (Employee) em.createQuery("SELECT DISTINCT e FROM " + Employee.class.getName() + " e WHERE e.firstName = 'Fred'").getSingleResult();
            // Do a MEMBER OF query using an input parameter
            List result = em.createQuery("SELECT DISTINCT m FROM " + Manager.class.getName() + " m " + "WHERE :param MEMBER OF m.subordinates").setParameter("param", emp).getResultList();
            // Manager 1
            assertEquals(1, result.size());
            Manager mgr = (Manager) result.get(0);
            assertEquals("Manager returned from MEMBER OF query has incorrect firstName", "Chief", mgr.getFirstName());
            assertEquals("Manager returned from MEMBER OF query has incorrect lastName", "Rock", mgr.getLastName());
            // Do a NOT MEMBER OF query using an input parameter
            result = em.createQuery("SELECT DISTINCT m FROM " + Manager.class.getName() + " m WHERE :param NOT MEMBER OF m.subordinates").setParameter("param", emp).getResultList();
            // Manager 2
            assertEquals(1, result.size());
            mgr = (Manager) result.get(0);
            assertEquals("Manager returned from NOT MEMBER OF query has incorrect firstName", "Boss", mgr.getFirstName());
            assertEquals("Manager returned from NOT MEMBER OF query has incorrect lastName", "Blaster", mgr.getLastName());
            // Do a MEMBER OF query using a field
            result = em.createQuery("SELECT DISTINCT m FROM " + Manager.class.getName() + " m WHERE m.bestFriend MEMBER OF m.subordinates").getResultList();
            // Manager 1
            assertEquals(1, result.size());
            mgr = (Manager) result.get(0);
            assertEquals("Manager returned from MEMBER OF query has incorrect firstName", "Chief", mgr.getFirstName());
            assertEquals("Manager returned from MEMBER OF query has incorrect lastName", "Rock", mgr.getLastName());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown generating query with MEMBER syntax " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        removePersonBestFriendRelation();
        removeManagerEmployeeRelation();
        clean(Manager.class);
        clean(Employee.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) List(java.util.List) ArrayList(java.util.ArrayList) StoreManager(org.datanucleus.store.StoreManager) Manager(org.datanucleus.samples.annotations.models.company.Manager) EntityManager(javax.persistence.EntityManager) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException) PersistenceException(javax.persistence.PersistenceException)

Example 8 with Employee

use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.

the class JPQLQueryTest method testHintExcludeSubclasses.

/**
 * Test for DN extension "excludeSubclasses".
 */
public void testHintExcludeSubclasses() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@gmail.com");
            em.persist(p1);
            Employee e2 = new Employee(102, "Barney", "Rubble", "barney.rubble@gmail.com", 10000f, "12300");
            em.persist(e2);
            em.flush();
            // No hint
            Query q0 = em.createQuery("SELECT p FROM Person_Ann p");
            org.datanucleus.store.query.Query intQuery0 = q0.unwrap(org.datanucleus.store.query.Query.class);
            assertTrue(intQuery0.isSubclasses());
            List<Person> results0 = q0.getResultList();
            assertEquals(2, results0.size());
            // hint = true
            Query q1 = em.createQuery("SELECT p FROM Person_Ann p");
            q1.setHint(org.datanucleus.store.query.Query.EXTENSION_EXCLUDE_SUBCLASSES, "true");
            org.datanucleus.store.query.Query intQuery1 = q1.unwrap(org.datanucleus.store.query.Query.class);
            assertFalse(intQuery1.isSubclasses());
            List<Person> results1 = q1.getResultList();
            assertEquals(1, results1.size());
            Person p = results1.get(0);
            assertEquals("Fred", p.getFirstName());
            assertEquals("Flintstone", p.getLastName());
            assertEquals(101, p.getPersonNum());
            // hint = false
            Query q2 = em.createQuery("SELECT p FROM Person_Ann p");
            q2.setHint(org.datanucleus.store.query.Query.EXTENSION_EXCLUDE_SUBCLASSES, "false");
            org.datanucleus.store.query.Query intQuery2 = q2.unwrap(org.datanucleus.store.query.Query.class);
            assertTrue(intQuery2.isSubclasses());
            List<Person> results2 = q2.getResultList();
            assertEquals(2, results2.size());
            tx.rollback();
        } catch (PersistenceException e) {
            LOG.error("Exception in test", e);
            fail("Exception in Named Query test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) PersistenceException(javax.persistence.PersistenceException) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 9 with Employee

use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.

the class JPQLQueryTest method testCASESimple.

public void testCASESimple() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(105, "Pebbles", "Flintstone", "pebbles.flintstone@datanucleus.org");
            p.setAge(5);
            em.persist(p);
            Employee e = new Employee(106, "Barney", "Rubble", "barney.rubble@jpox.com", 10000.0f, "12345");
            e.setAge(35);
            em.persist(e);
            em.flush();
            List result = em.createQuery("SELECT p.personNum, CASE p.age WHEN 5 THEN '5-yr old' ELSE 'Other' END FROM " + Person.class.getName() + " p").getResultList();
            assertEquals("Number of results is incorrect", 2, result.size());
            Iterator resultsIter = result.iterator();
            boolean pebbles = false;
            boolean barney = false;
            while (resultsIter.hasNext()) {
                Object[] values = (Object[]) resultsIter.next();
                int idValue = ((Number) values[0]).intValue();
                String caseValue = (String) values[1];
                if (idValue == 105 && caseValue.equals("5-yr old")) {
                    pebbles = true;
                }
                if (// HSQL 2.x pads the string to the same length (i.e CHAR not VARCHAR)
                idValue == 106 && (caseValue.equals("Other") || caseValue.equals("Other   "))) {
                    barney = true;
                }
            }
            assertTrue("Pebbles wasn't correct in the Case results", pebbles);
            assertTrue("Barney wasn't correct in the Case results", barney);
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException) PersistenceException(javax.persistence.PersistenceException) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 10 with Employee

use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.

the class JPQLQueryTest method testOrderByResultAlias.

/**
 * Test of the use of a result alias, and using it in ORDER BY.
 */
public void testOrderByResultAlias() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Employee e1 = new Employee(101, "Fred", "Flintstone", "fred.flintstone@jpox.com", 30000f, "1234A");
            Employee e2 = new Employee(102, "George", "Cement", "george.cement@jpox.com", 20000f, "1235C");
            em.persist(e1);
            em.persist(e2);
            em.flush();
            List<Object[]> result = em.createQuery("SELECT e.firstName, (e.salary/12) AS MONTHLY FROM " + Employee.class.getName() + " e ORDER BY MONTHLY ASC").getResultList();
            assertEquals(2, result.size());
            Object[] result1 = result.get(0);
            Object[] result2 = result.get(1);
            assertEquals("George", result1[0]);
            assertEquals(1666.6666, ((Number) result1[1]).doubleValue(), 0.1);
            assertEquals("Fred", result2[0]);
            assertEquals(2500.00, ((Number) result2[1]).doubleValue(), 0.1);
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Employee.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee)

Aggregations

Employee (org.datanucleus.samples.annotations.models.company.Employee)20 EntityManager (javax.persistence.EntityManager)19 EntityTransaction (javax.persistence.EntityTransaction)19 Query (javax.persistence.Query)11 TypedQuery (javax.persistence.TypedQuery)9 Person (org.datanucleus.samples.annotations.models.company.Person)8 PersistenceException (javax.persistence.PersistenceException)7 Manager (org.datanucleus.samples.annotations.models.company.Manager)7 NoResultException (javax.persistence.NoResultException)6 NonUniqueResultException (javax.persistence.NonUniqueResultException)6 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Path (javax.persistence.criteria.Path)4 Predicate (javax.persistence.criteria.Predicate)3 StoreManager (org.datanucleus.store.StoreManager)3 Iterator (java.util.Iterator)2 Join (javax.persistence.criteria.Join)2 Attribute (javax.persistence.metamodel.Attribute)2