Search in sources :

Example 6 with Attribute

use of javax.persistence.metamodel.Attribute in project tests by datanucleus.

the class MetamodelTest method testEmbedded.

/**
 * Test for embedded types with metamodel.
 */
public void testEmbedded() {
    Metamodel model = emf.getMetamodel();
    try {
        EntityType<?> computerType = model.entity(Computer.class);
        assertNotNull(computerType);
        assertEquals("Number of attributes is wrong", 4, computerType.getAttributes().size());
        Class idType = computerType.getIdType().getJavaType();
        assertEquals(long.class, idType);
        try {
            // String field (Id)
            Attribute attr = computerType.getAttribute("soundCard");
            assertNotNull(attr);
            assertEquals(attr.getName(), "soundCard");
            assertEquals(attr.getJavaType(), ComputerCard.class);
            assertEquals(attr.getJavaMember().getName(), "soundCard");
            assertFalse(attr.isCollection());
            assertTrue(attr.isAssociation());
            assertEquals(Attribute.PersistentAttributeType.EMBEDDED, attr.getPersistentAttributeType());
            assertTrue(attr instanceof SingularAttribute);
            SingularAttribute sattr = (SingularAttribute) attr;
            assertTrue(sattr.isOptional());
            assertFalse(sattr.isVersion());
            assertEquals(BindableType.SINGULAR_ATTRIBUTE, sattr.getBindableType());
            assertEquals(ComputerCard.class, sattr.getBindableJavaType());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"soundCard\" field of " + Computer.class.getName());
        }
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + Computer.class.getName());
    }
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Computer(org.datanucleus.samples.annotations.embedded.Computer) Metamodel(javax.persistence.metamodel.Metamodel)

Example 7 with Attribute

use of javax.persistence.metamodel.Attribute in project tests by datanucleus.

the class MetamodelTest method testMap.

/**
 * Test for class with Map field(s).
 */
public void testMap() {
    Metamodel model = emf.getMetamodel();
    try {
        EntityType<?> mapHolderType = model.entity(MapJoinHolder.class);
        assertNotNull(mapHolderType);
        assertEquals("Number of attributes is wrong", 6, mapHolderType.getAttributes().size());
        try {
            // long field (id)
            Attribute attr = mapHolderType.getAttribute("id");
            assertNotNull(attr);
            assertTrue(attr instanceof SingularAttribute);
            assertEquals("id", attr.getName());
            assertEquals(long.class, attr.getJavaType());
            assertEquals("id", attr.getJavaMember().getName());
            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 \"id\" field of " + MapJoinHolder.class.getName());
        }
        try {
            // String field (name)
            Attribute attr = mapHolderType.getAttribute("name");
            assertNotNull(attr);
            assertTrue(attr instanceof SingularAttribute);
            assertEquals("name", attr.getName());
            assertEquals(String.class, attr.getJavaType());
            assertEquals("name", attr.getJavaMember().getName());
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
            assertTrue(attr instanceof SingularAttribute);
            assertTrue(((SingularAttribute) attr).isOptional());
            assertFalse(((SingularAttribute) attr).isVersion());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"name\" field of " + MapJoinHolder.class.getName());
        }
        try {
            // Map<NonPC, PC>
            Attribute attr = mapHolderType.getAttribute("map");
            assertNotNull(attr);
            assertTrue(attr instanceof MapAttribute);
            MapAttribute mapAttr = (MapAttribute) attr;
            assertEquals("map", attr.getName());
            assertEquals(Map.class, attr.getJavaType());
            assertEquals("map", attr.getJavaMember().getName());
            assertTrue(attr.isCollection());
            assertTrue(attr.isAssociation());
            assertEquals(String.class.getName(), mapAttr.getKeyJavaType().getName());
            Type valueType = mapAttr.getElementType();
            assertEquals(MapJoinValue.class.getName(), valueType.getJavaType().getName());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"map\" field of " + MapJoinHolder.class.getName());
        }
        try {
            // Map<NonPC, NonPC>
            Attribute attr = mapHolderType.getAttribute("map2");
            assertNotNull(attr);
            assertTrue(attr instanceof MapAttribute);
            MapAttribute mapAttr = (MapAttribute) attr;
            assertEquals("map2", attr.getName());
            assertEquals(Map.class, attr.getJavaType());
            assertEquals("map2", attr.getJavaMember().getName());
            assertTrue(attr.isCollection());
            assertFalse(attr.isAssociation());
            assertEquals(Integer.class.getName(), mapAttr.getKeyJavaType().getName());
            Type valueType = mapAttr.getElementType();
            assertEquals(String.class.getName(), valueType.getJavaType().getName());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"map2\" field of " + MapJoinHolder.class.getName());
        }
        try {
            // Map<NonPC, PC>
            Attribute attr = mapHolderType.getAttribute("map3");
            assertNotNull(attr);
            assertTrue(attr instanceof MapAttribute);
            MapAttribute mapAttr = (MapAttribute) attr;
            assertEquals("map3", attr.getName());
            assertEquals(Map.class, attr.getJavaType());
            assertEquals("map3", attr.getJavaMember().getName());
            assertTrue(attr.isCollection());
            assertTrue(attr.isAssociation());
            assertEquals(String.class.getName(), mapAttr.getKeyJavaType().getName());
            Type valueType = mapAttr.getElementType();
            assertEquals(MapJoinEmbeddedValue.class.getName(), valueType.getJavaType().getName());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"map3\" field of " + MapJoinHolder.class.getName());
        }
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + Animal.class.getName());
    }
}
Also used : MapJoinEmbeddedValue(org.datanucleus.samples.annotations.one_many.map_join.MapJoinEmbeddedValue) MapJoinValue(org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) MapJoinHolder(org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder) SingularAttribute(javax.persistence.metamodel.SingularAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) EntityType(javax.persistence.metamodel.EntityType) Type(javax.persistence.metamodel.Type) BindableType(javax.persistence.metamodel.Bindable.BindableType) IdentifiableType(javax.persistence.metamodel.IdentifiableType) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Metamodel(javax.persistence.metamodel.Metamodel)

Example 8 with Attribute

use of javax.persistence.metamodel.Attribute in project tests by datanucleus.

the class MetamodelTest method testManyToOne.

/**
 * Test for the identification of relation type.
 */
public void testManyToOne() {
    Metamodel model = emf.getMetamodel();
    try {
        EntityType<?> ownerType = model.entity(ManyOneOwner.class);
        assertNotNull(ownerType);
        assertEquals("Number of attributes is wrong", 2, ownerType.getAttributes().size());
        try {
            Attribute attr = ownerType.getAttribute("other");
            assertNotNull(attr);
            assertEquals(attr.getName(), "other");
            assertFalse(attr.isCollection());
            assertTrue(attr instanceof SingularAttribute);
            assertEquals(Attribute.PersistentAttributeType.MANY_TO_ONE, attr.getPersistentAttributeType());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"other\" field of " + ManyOneOwner.class.getName());
        }
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + ManyOneOwner.class.getName());
    }
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Metamodel(javax.persistence.metamodel.Metamodel) ManyOneOwner(org.datanucleus.samples.annotations.many_one.ManyOneOwner)

Example 9 with Attribute

use of javax.persistence.metamodel.Attribute in project tests by datanucleus.

the class CriteriaStringsTest method testBasicWithFromJoinOneToOne.

/**
 * Test basic generation of query with candidate and alias, and FROM join on 1-1.
 */
public void testBasicWithFromJoinOneToOne() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Employee> crit = cb.createQuery(Employee.class);
        Root<Employee> candidate = crit.from(Employee.class);
        candidate.alias("e");
        crit.select(candidate);
        Metamodel model = emf.getMetamodel();
        ManagedType empType = model.managedType(Employee.class);
        Attribute bAttr = empType.getAttribute("account");
        Join accountJoin = candidate.join((SingularAttribute) bAttr);
        accountJoin.alias("a");
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT e FROM org.datanucleus.samples.annotations.models.company.Employee e JOIN e.account a", crit.toString());
        Query q = em.createQuery(crit);
        List<Employee> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 2, results.size());
        Iterator<Employee> iter = results.iterator();
        boolean joeExists = false;
        boolean nigelExists = false;
        while (iter.hasNext()) {
            Employee emp = iter.next();
            if (emp.getFirstName().equals("Nigel") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 106) {
                nigelExists = true;
            } else if (emp.getFirstName().equals("Joe") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 105) {
                joeExists = true;
            }
        }
        assertTrue("Nigel not present", nigelExists);
        assertTrue("Joe not present", joeExists);
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) ManagedType(javax.persistence.metamodel.ManagedType) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Join(javax.persistence.criteria.Join) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) Metamodel(javax.persistence.metamodel.Metamodel)

Example 10 with Attribute

use of javax.persistence.metamodel.Attribute in project tests by datanucleus.

the class CriteriaStringsTest method testBasicWithFromJoinOneToMany.

/**
 * Test basic generation of query with candidate and alias, and FROM join on 1-N.
 */
public void testBasicWithFromJoinOneToMany() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Farm> crit = cb.createQuery(Farm.class);
        Root<Farm> candidate = crit.from(Farm.class);
        candidate.alias("f");
        crit.select(candidate);
        Metamodel model = emf.getMetamodel();
        ManagedType farmType = model.managedType(Farm.class);
        Attribute animalAttr = farmType.getAttribute("animals");
        Join animalJoin = candidate.join((ListAttribute) animalAttr);
        animalJoin.alias("a");
        Path nameField = animalJoin.get("name");
        Predicate nameEquals = cb.equal(nameField, "Woolly Sheep");
        crit.where(nameEquals);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT f FROM org.datanucleus.samples.annotations.one_many.bidir.Farm f JOIN f.animals a WHERE (a.name = 'Woolly Sheep')", crit.toString());
        Query q = em.createQuery(crit);
        List<Farm> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 1, results.size());
        Farm farm = (Farm) results.get(0);
        assertEquals("Farm is incorrect", "Kiwi Farm", farm.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) ManagedType(javax.persistence.metamodel.ManagedType) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Join(javax.persistence.criteria.Join) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm) Metamodel(javax.persistence.metamodel.Metamodel)

Aggregations

Attribute (javax.persistence.metamodel.Attribute)31 SingularAttribute (javax.persistence.metamodel.SingularAttribute)21 ListAttribute (javax.persistence.metamodel.ListAttribute)13 PluralAttribute (javax.persistence.metamodel.PluralAttribute)12 MapAttribute (javax.persistence.metamodel.MapAttribute)10 Metamodel (javax.persistence.metamodel.Metamodel)9 CollectionAttribute (javax.persistence.metamodel.CollectionAttribute)6 SetAttribute (javax.persistence.metamodel.SetAttribute)6 Path (javax.persistence.criteria.Path)5 EntityType (javax.persistence.metamodel.EntityType)5 ManagedType (javax.persistence.metamodel.ManagedType)5 EntityManager (javax.persistence.EntityManager)4 TypedQuery (javax.persistence.TypedQuery)4 Predicate (javax.persistence.criteria.Predicate)4 Field (java.lang.reflect.Field)3 Expression (javax.persistence.criteria.Expression)3 From (javax.persistence.criteria.From)3 Root (javax.persistence.criteria.Root)3 IdentifiableType (javax.persistence.metamodel.IdentifiableType)3 Type (javax.persistence.metamodel.Type)3