Search in sources :

Example 6 with Animal

use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.

the class CriteriaStringsTest method testProjectionOnManyToOne.

/**
 * Test projection of many-to-one field.
 */
public void testProjectionOnManyToOne() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Farm> crit = cb.createQuery(Farm.class);
        Root<Animal> candidate = crit.from(Animal.class);
        candidate.alias("a");
        Path<Farm> farmPath = candidate.get("farm");
        crit.select(farmPath);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT a.farm FROM org.datanucleus.samples.annotations.one_many.bidir.Animal a", crit.toString());
        Query q = em.createQuery(crit);
        List<Farm> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 3, results.size());
        Object result = results.get(0);
        assertNotNull(result);
        assertTrue("Result is of incorrect type", result instanceof Farm);
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm)

Example 7 with Animal

use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.

the class JPQLQueryTest method testProjectionOfManyToOneField.

/**
 * Test projection of N-1 field.
 */
public void testProjectionOfManyToOneField() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Farm farm1 = new Farm("High Farm");
            Farm farm2 = new Farm("Low Farm");
            Animal a1 = new Animal("Dog");
            Animal a2 = new Animal("Sheep");
            Animal a3 = new Animal("Cow");
            farm1.getAnimals().add(a1);
            farm1.getAnimals().add(a2);
            farm2.getAnimals().add(a3);
            a1.setFarm(farm1);
            a2.setFarm(farm1);
            a3.setFarm(farm2);
            em.persist(farm1);
            em.persist(farm2);
            em.flush();
            List results = em.createQuery("SELECT a.farm FROM " + Animal.class.getName() + " a ", Farm.class).getResultList();
            assertEquals(3, results.size());
            Object result = results.get(0);
            assertNotNull(result);
            assertTrue("Result is of incorrect type", result instanceof Farm);
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm) List(java.util.List) ArrayList(java.util.ArrayList)

Example 8 with Animal

use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.

the class JPQLQueryTest method testResultAggregate.

/**
 * Test for result aggregates.
 */
public void testResultAggregate() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Farm farm1 = new Farm("High Farm");
            Farm farm2 = new Farm("Low Farm");
            Animal a1 = new Animal("Dog");
            Animal a2 = new Animal("Sheep");
            Animal a3 = new Animal("Cow");
            farm1.getAnimals().add(a1);
            farm1.getAnimals().add(a2);
            farm2.getAnimals().add(a3);
            em.persist(farm1);
            em.persist(farm2);
            em.flush();
            Object result = em.createQuery("SELECT COUNT(F) FROM " + Farm.class.getName() + " F").getSingleResult();
            assertTrue(result instanceof Long);
            assertEquals(2, ((Long) result).longValue());
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm)

Example 9 with Animal

use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.

the class MetamodelTest method testBasic.

/**
 * Test for the list of classes and their basic model info
 */
public void testBasic() {
    Metamodel model = emf.getMetamodel();
    try {
        EntityType<?> animalType = model.entity(Animal.class);
        assertNotNull(animalType);
        assertEquals("Number of Animal attributes is wrong", 2, animalType.getAttributes().size());
        Class idType = animalType.getIdType().getJavaType();
        assertEquals(String.class, idType);
        try {
            // String field (Id)
            Attribute attr = animalType.getAttribute("name");
            assertNotNull(attr);
            assertEquals(attr.getName(), "name");
            assertEquals(attr.getJavaType(), String.class);
            assertEquals(attr.getJavaMember().getName(), "name");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
            assertTrue(attr instanceof SingularAttribute);
            assertFalse(((SingularAttribute) attr).isOptional());
            assertFalse(((SingularAttribute) attr).isVersion());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"name\" field of " + Animal.class.getName());
        }
        try {
            // N-1 field
            Attribute attr = animalType.getAttribute("farm");
            assertNotNull(attr);
            assertEquals(attr.getName(), "farm");
            assertEquals(attr.getJavaType(), Farm.class);
            assertEquals(attr.getJavaMember().getName(), "farm");
            assertFalse(attr.isCollection());
            assertTrue(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"farm\" field of " + Animal.class.getName());
        }
        // Supertype should be null
        assertNull(animalType.getSupertype());
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + Animal.class.getName());
    }
    try {
        EntityType<?> farmType = model.entity(Farm.class);
        assertNotNull(farmType);
        assertEquals("Number of Farm attributes is wrong", 2, farmType.getAttributes().size());
        try {
            // String field
            Attribute attr = farmType.getAttribute("name");
            assertNotNull(attr);
            assertEquals(attr.getName(), "name");
            assertEquals(attr.getJavaType(), String.class);
            assertEquals(attr.getJavaMember().getName(), "name");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"name\" field of " + Farm.class.getName());
        }
        try {
            // N-1 field
            Attribute attr = farmType.getAttribute("animals");
            assertNotNull(attr);
            assertEquals(attr.getName(), "animals");
            assertEquals(attr.getJavaType(), ArrayList.class);
            assertEquals(attr.getJavaMember().getName(), "animals");
            assertTrue(attr.isCollection());
            assertTrue(attr.isAssociation());
            assertTrue("Attribute for animals is not castable to ListAttribute!", attr instanceof ListAttribute);
            ListAttribute listAttr = (ListAttribute) attr;
            Type elemType = listAttr.getElementType();
            assertEquals("Element type is wrong", Animal.class, elemType.getJavaType());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"animals\" field of " + Farm.class.getName());
        }
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + Farm.class.getName());
    }
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) EntityType(javax.persistence.metamodel.EntityType) Type(javax.persistence.metamodel.Type) BindableType(javax.persistence.metamodel.Bindable.BindableType) IdentifiableType(javax.persistence.metamodel.IdentifiableType) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm) Metamodel(javax.persistence.metamodel.Metamodel) ListAttribute(javax.persistence.metamodel.ListAttribute)

Example 10 with Animal

use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.

the class MultithreadedTest method processQueryAndDetachOneManyFKBidir.

protected String processQueryAndDetachOneManyFKBidir(boolean transaction) {
    List<Animal> results = null;
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        if (transaction) {
            tx.begin();
        }
        Query q = em.createQuery("SELECT a FROM " + Animal.class.getName() + " a");
        List<Animal> animals = q.getResultList();
        for (Animal a : animals) {
            Farm f = a.getFarm();
            f.getAnimals();
        }
        results = new ArrayList<Animal>(animals);
        if (transaction) {
            tx.commit();
        }
    } catch (Throwable thr) {
        LOG.error("Exception query objects", thr);
        return "Exception in query : " + thr.getMessage();
    } finally {
        if (transaction && tx.isActive()) {
            tx.rollback();
        }
        // Detached the Animals and their loaded fields
        em.close();
    }
    for (Animal a : results) {
        try {
            LOG.debug(">> Animal: " + a);
            if (a.getFarm() == null) {
                return "Animal has null Farm";
            }
            Farm f = a.getFarm();
            if (f.getAnimals() == null || f.getAnimals().size() != 100) {
                return "Animal has Farm with incorrect Animals";
            }
        } catch (Exception exc) {
            LOG.error(">> Exception thrown on check of results", exc);
            return "Exception checking results : " + exc.getMessage();
        }
    }
    return null;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm)

Aggregations

Animal (org.datanucleus.samples.annotations.one_many.bidir.Animal)11 Farm (org.datanucleus.samples.annotations.one_many.bidir.Farm)11 EntityManager (javax.persistence.EntityManager)10 EntityTransaction (javax.persistence.EntityTransaction)10 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Query (javax.persistence.Query)3 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 NoResultException (javax.persistence.NoResultException)1 NonUniqueResultException (javax.persistence.NonUniqueResultException)1 PersistenceException (javax.persistence.PersistenceException)1 TypedQuery (javax.persistence.TypedQuery)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Attribute (javax.persistence.metamodel.Attribute)1 BindableType (javax.persistence.metamodel.Bindable.BindableType)1 EntityType (javax.persistence.metamodel.EntityType)1 IdentifiableType (javax.persistence.metamodel.IdentifiableType)1 ListAttribute (javax.persistence.metamodel.ListAttribute)1