Search in sources :

Example 1 with ListAttribute

use of javax.persistence.metamodel.ListAttribute 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)

Example 2 with ListAttribute

use of javax.persistence.metamodel.ListAttribute in project hibernate-orm by hibernate.

the class AbstractFromImpl method joinList.

@Override
@SuppressWarnings({ "unchecked" })
public <X, Y> ListJoin<X, Y> joinList(String attributeName, JoinType jt) {
    final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute(attributeName);
    if (!attribute.isCollection()) {
        throw new IllegalArgumentException("Requested attribute was not a list");
    }
    final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
    if (!PluralAttribute.CollectionType.LIST.equals(pluralAttribute.getCollectionType())) {
        throw new IllegalArgumentException("Requested attribute was not a list");
    }
    return (ListJoin<X, Y>) join((ListAttribute) attribute, jt);
}
Also used : ListJoin(javax.persistence.criteria.ListJoin) CollectionAttribute(javax.persistence.metamodel.CollectionAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) SetAttribute(javax.persistence.metamodel.SetAttribute) ListAttribute(javax.persistence.metamodel.ListAttribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) ListAttribute(javax.persistence.metamodel.ListAttribute)

Example 3 with ListAttribute

use of javax.persistence.metamodel.ListAttribute 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)

Aggregations

Attribute (javax.persistence.metamodel.Attribute)3 ListAttribute (javax.persistence.metamodel.ListAttribute)3 SingularAttribute (javax.persistence.metamodel.SingularAttribute)3 MapAttribute (javax.persistence.metamodel.MapAttribute)2 Metamodel (javax.persistence.metamodel.Metamodel)2 Farm (org.datanucleus.samples.annotations.one_many.bidir.Farm)2 EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Join (javax.persistence.criteria.Join)1 ListJoin (javax.persistence.criteria.ListJoin)1 Path (javax.persistence.criteria.Path)1 Predicate (javax.persistence.criteria.Predicate)1 BindableType (javax.persistence.metamodel.Bindable.BindableType)1 CollectionAttribute (javax.persistence.metamodel.CollectionAttribute)1 EntityType (javax.persistence.metamodel.EntityType)1 IdentifiableType (javax.persistence.metamodel.IdentifiableType)1