Search in sources :

Example 6 with Qualification

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

the class CriteriaStringsTest method setUp.

/* (non-Javadoc)
     * @see org.datanucleus.tests.PersistenceTestCase#setUp()
     */
@Override
protected void setUp() throws Exception {
    super.setUp();
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@datanucleus.org");
        p1.setAge(35);
        em.persist(p1);
        Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@datanucleus.org");
        p2.setAge(38);
        p2.setBestFriend(p1);
        em.persist(p2);
        // 1-1 relation sample
        Employee emp1 = new Employee(105, "Joe", "Bloggs", "joe.bloggs@datanucleus.org", 12000.0f, "10005");
        emp1.setAge(40);
        Employee emp2 = new Employee(106, "Nigel", "Bloggs", "nigel.bloggs@datanucleus.org", 13000.0f, "10006");
        emp2.setAge(41);
        Account acc1 = new Account();
        acc1.setId(105);
        acc1.setUsername("joebloggs");
        acc1.setEnabled(true);
        Account acc2 = new Account();
        acc2.setId(106);
        acc2.setUsername("nigelbloggs");
        acc2.setEnabled(true);
        emp1.setAccount(acc1);
        emp2.setAccount(acc2);
        Qualification q1 = new Qualification("BSc");
        Calendar cal = GregorianCalendar.getInstance();
        cal.set(Calendar.YEAR, 2001);
        cal.set(Calendar.MONTH, 5);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        q1.setDate(cal.getTime());
        q1.setPerson(emp1);
        Qualification q2 = new Qualification("MSc");
        q2.setPerson(emp2);
        cal.set(Calendar.YEAR, 2011);
        cal.set(Calendar.MONTH, 4);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        q2.setDate(cal.getTime());
        em.persist(emp1);
        em.persist(emp2);
        em.persist(q1);
        em.persist(q2);
        // 1-N relation sample
        Farm farm1 = new Farm("Giles Farm");
        Farm farm2 = new Farm("Kiwi Farm");
        Animal an1 = new Animal("Brown Cow");
        Animal an2 = new Animal("Woolly Sheep");
        Animal an3 = new Animal("Sheepdog");
        farm1.getAnimals().add(an1);
        an1.setFarm(farm1);
        farm2.getAnimals().add(an2);
        an2.setFarm(farm2);
        farm2.getAnimals().add(an3);
        an3.setFarm(farm2);
        em.persist(farm1);
        em.persist(farm2);
        tx.commit();
    } catch (Exception e) {
        LOG.error(">> Exception in setUp data", e);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) EntityManager(javax.persistence.EntityManager) Qualification(org.datanucleus.samples.annotations.models.company.Qualification) Employee(org.datanucleus.samples.annotations.models.company.Employee) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 7 with Qualification

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

the class CriteriaStringsTest method testBasicWithFilterLessThanDateLiteral.

/**
 * Test basic generation of query with candidate and alias and filter (<).
 */
public void testBasicWithFilterLessThanDateLiteral() {
    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, 2006);
        cal.set(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date date = new Date(cal.getTime().getTime());
        Path dateField = candidate.get("date");
        Predicate dateLessThan = cb.lessThan(dateField, date);
        crit.where(dateLessThan);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT q FROM org.datanucleus.samples.annotations.models.company.Qualification q WHERE (q.date < {d '2006-02-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());
        Qualification qual = results.iterator().next();
        assertEquals("BSc", qual.getName());
        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) Predicate(javax.persistence.criteria.Predicate)

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