Search in sources :

Example 1 with Qualification

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

the class JPQLQueryTest method testWhereTREAT.

/**
 * Test for TREAT in WHERE clause.
 */
public void testWhereTREAT() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
            Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
            Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
            Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
            bart.addSubordinate(boss);
            bart.addSubordinate(boss2);
            Qualification q1 = new Qualification("q1");
            q1.setPerson(boss);
            Qualification q2 = new Qualification("q2");
            q2.setPerson(boss2);
            em.persist(bart);
            em.persist(homer);
            em.persist(boss);
            em.persist(boss2);
            em.persist(q1);
            em.persist(q2);
            em.flush();
            List<Qualification> results = em.createQuery("SELECT q FROM " + Qualification.class.getName() + " q WHERE (TREAT(person AS Employee_Ann)).serialNo = \"serial 3\"").getResultList();
            assertEquals(1, results.size());
            assertEquals("q1", ((Qualification) results.iterator().next()).getName());
            tx.rollback();
        } catch (PersistenceException e) {
            LOG.error("Exception in test", e);
            fail("Exception in TREAT WHERE test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        removeManagerEmployeeRelation();
        clean(Qualification.class);
        clean(Manager.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Qualification(org.datanucleus.samples.annotations.models.company.Qualification) PersistenceException(javax.persistence.PersistenceException) StoreManager(org.datanucleus.store.StoreManager) Manager(org.datanucleus.samples.annotations.models.company.Manager) EntityManager(javax.persistence.EntityManager)

Example 2 with Qualification

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

the class JPQLQueryTest method testJoinOnWithSubquery.

/**
 * Test use of JOIN ON where the ON clause contains a subquery.
 */
public void testJoinOnWithSubquery() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@dn.org");
            p1.setAge(31);
            em.persist(p1);
            Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@dn.org");
            p2.setAge(35);
            em.persist(p2);
            Qualification q1 = new Qualification("BSc in Maths");
            q1.setPerson(p1);
            q1.setDate(new Date());
            em.flush();
            // Just check that this is queryable
            Query q = em.createQuery("SELECT q.name FROM " + Qualification.class.getName() + " q JOIN q.person p ON p.age < (SELECT AVG(p2.age) FROM " + Person.class.getName() + " p2)");
            q.getResultList();
            // TODO Add some asserts, or choose a good example for this
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Qualification.class);
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Qualification(org.datanucleus.samples.annotations.models.company.Qualification) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) Person(org.datanucleus.samples.annotations.models.company.Person) Date(java.util.Date)

Example 3 with Qualification

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

the class JPQLQueryTest method testFromTREAT.

/**
 * Test for TREAT in FROM clause.
 */
public void testFromTREAT() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
            Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
            Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
            Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
            bart.addSubordinate(boss);
            bart.addSubordinate(boss2);
            Qualification q1 = new Qualification("q1");
            q1.setPerson(boss);
            Qualification q2 = new Qualification("q2");
            q2.setPerson(boss2);
            em.persist(bart);
            em.persist(homer);
            em.persist(boss);
            em.persist(boss2);
            em.persist(q1);
            em.persist(q2);
            em.flush();
            List<Qualification> results = em.createQuery("SELECT q FROM " + Qualification.class.getName() + " q JOIN TREAT(q.person AS " + Employee.class.getName() + ") e WHERE e.serialNo = \"serial 3\"").getResultList();
            assertEquals(1, results.size());
            assertEquals("q1", ((Qualification) results.iterator().next()).getName());
            tx.rollback();
        } catch (PersistenceException e) {
            LOG.error("Exception in test", e);
            fail("Exception in TREAT FROM test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        removeManagerEmployeeRelation();
        clean(Qualification.class);
        clean(Manager.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Qualification(org.datanucleus.samples.annotations.models.company.Qualification) PersistenceException(javax.persistence.PersistenceException) StoreManager(org.datanucleus.store.StoreManager) Manager(org.datanucleus.samples.annotations.models.company.Manager) EntityManager(javax.persistence.EntityManager)

Example 4 with Qualification

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

the class CriteriaStringsTest method testTREATInWhereClause.

/**
 * Test generation of filter with TREAT.
 */
public void testTREATInWhereClause() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Qualification> crit = cb.createQuery(Qualification.class);
        Root<Qualification> candidate = crit.from(Qualification.class);
        candidate.alias("q");
        crit.select(candidate);
        Path<Employee> empPath = cb.treat(candidate.get("person"), Employee.class);
        Path agePath = empPath.get("age");
        crit.where(cb.gt(agePath, 40));
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT q FROM org.datanucleus.samples.annotations.models.company.Qualification q WHERE (TREAT(q.person AS " + Employee.class.getName() + ").age > 40)", crit.toString());
        Query q = em.createQuery(crit);
        List<Qualification> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 1, results.size());
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Qualification(org.datanucleus.samples.annotations.models.company.Qualification) Employee(org.datanucleus.samples.annotations.models.company.Employee) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query)

Example 5 with Qualification

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

the class CriteriaStringsTest method testLessThanDate.

/**
 * Test less than Date.
 */
public void testLessThanDate() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Qualification> crit = cb.createQuery(Qualification.class);
        Root<Qualification> candidate = crit.from(Qualification.class);
        candidate.alias("q");
        crit.select(candidate);
        Calendar cal = GregorianCalendar.getInstance();
        cal.set(Calendar.YEAR, 2011);
        cal.set(Calendar.MONTH, 2);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 12);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Path datePath = candidate.get("date");
        crit.where(cb.lessThan(datePath, new java.sql.Date(cal.getTime().getTime())));
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT q FROM org.datanucleus.samples.annotations.models.company.Qualification q WHERE (q.date < {d '2011-03-01'})", crit.toString());
        Query q = em.createQuery(crit);
        List<Qualification> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 1, results.size());
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Qualification(org.datanucleus.samples.annotations.models.company.Qualification) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Date(java.sql.Date)

Aggregations

EntityManager (javax.persistence.EntityManager)7 EntityTransaction (javax.persistence.EntityTransaction)7 Qualification (org.datanucleus.samples.annotations.models.company.Qualification)7 Query (javax.persistence.Query)4 TypedQuery (javax.persistence.TypedQuery)4 Calendar (java.util.Calendar)3 GregorianCalendar (java.util.GregorianCalendar)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)3 Path (javax.persistence.criteria.Path)3 Date (java.sql.Date)2 PersistenceException (javax.persistence.PersistenceException)2 Employee (org.datanucleus.samples.annotations.models.company.Employee)2 Manager (org.datanucleus.samples.annotations.models.company.Manager)2 Person (org.datanucleus.samples.annotations.models.company.Person)2 StoreManager (org.datanucleus.store.StoreManager)2 Date (java.util.Date)1 Predicate (javax.persistence.criteria.Predicate)1 JPAQuery (org.datanucleus.api.jpa.JPAQuery)1 Account (org.datanucleus.samples.annotations.models.company.Account)1