Search in sources :

Example 26 with Attribute

use of javax.persistence.metamodel.Attribute 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 27 with Attribute

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

the class MetamodelTest method testInheritance.

/**
 * Test for the case with inheritance.
 */
public void testInheritance() {
    Metamodel model = emf.getMetamodel();
    try {
        EntityType<?> mgrType = model.entity(Manager.class);
        assertNotNull(mgrType);
        assertEquals("Number of Manager attributes is wrong", 16, mgrType.getAttributes().size());
        assertEquals("Number of Manager singularAttributes is wrong", 13, mgrType.getSingularAttributes().size());
        assertEquals("Number of Manager pluralAttributes is wrong", 3, mgrType.getPluralAttributes().size());
        try {
            // Field in Manager
            Attribute attr = mgrType.getAttribute("subordinates");
            assertNotNull(attr);
            assertEquals(attr.getName(), "subordinates");
            assertEquals(attr.getJavaType(), Set.class);
            assertEquals(attr.getJavaMember().getName(), "subordinates");
            assertTrue(attr.isCollection());
            assertTrue(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"subordinates\" field of " + Manager.class.getName());
        }
        try {
            // Field in Employee
            Attribute attr = mgrType.getAttribute("serialNo");
            assertNotNull(attr);
            assertEquals(attr.getName(), "serialNo");
            assertEquals(attr.getJavaType(), String.class);
            assertEquals(attr.getJavaMember().getName(), "serialNo");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"serialNo\" field of " + Employee.class.getName());
        }
        try {
            // Primitive Field in Employee
            Attribute attr = mgrType.getAttribute("salary");
            assertNotNull(attr);
            assertEquals(attr.getName(), "salary");
            assertEquals(attr.getJavaType(), float.class);
            assertEquals(attr.getJavaMember().getName(), "salary");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
            assertTrue(attr instanceof SingularAttribute);
            assertFalse(((SingularAttribute) attr).isOptional());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"salary\" field of " + Employee.class.getName());
        }
        try {
            // Field in Person
            Attribute attr = mgrType.getAttribute("firstName");
            assertNotNull(attr);
            assertEquals(attr.getName(), "firstName");
            assertEquals(attr.getJavaType(), String.class);
            assertEquals(attr.getJavaMember().getName(), "firstName");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"firstName\" field of " + Person.class.getName());
        }
        // Supertype should not be null
        IdentifiableType empType = mgrType.getSupertype();
        assertNotNull(empType);
        IdentifiableType persType = empType.getSupertype();
        assertNotNull(persType);
        IdentifiableType superType = persType.getSupertype();
        assertNull(superType);
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + Manager.class.getName());
    }
    try {
        EntityType<?> mugType = model.entity(ModeratedUserGroup.class);
        assertNotNull(mugType);
        assertNotNull(mugType.getId(long.class));
        assertEquals("id", mugType.getId(long.class).getName());
    } catch (IllegalArgumentException iae) {
        fail("Error in metamodel tests" + iae.getMessage());
    }
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) VersionedEmployee(org.datanucleus.samples.annotations.versioned.VersionedEmployee) Employee(org.datanucleus.samples.annotations.models.company.Employee) 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) Manager(org.datanucleus.samples.annotations.models.company.Manager) Metamodel(javax.persistence.metamodel.Metamodel) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 28 with Attribute

use of javax.persistence.metamodel.Attribute in project midpoint by Evolveum.

the class GeneralUpdate method updateNameCopyAttribute.

private void updateNameCopyAttribute(Attribute attribute) {
    if ("name".equals(attribute.getName()) && RObject.class.isAssignableFrom(attribute.getDeclaringType().getJavaType())) {
        Attribute nameCopyAttribute = findAttributeForName("nameCopy");
        assert nameCopyAttribute != null;
        updateAttribute(nameCopyAttribute);
    }
}
Also used : RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) Attribute(javax.persistence.metamodel.Attribute)

Example 29 with Attribute

use of javax.persistence.metamodel.Attribute in project midpoint by Evolveum.

the class GeneralUpdate method handleItemDelta.

void handleItemDelta() throws SchemaException {
    while (segmentsIterator.hasNext()) {
        Object currentPathSegment = segmentsIterator.next();
        if (!ItemPath.isName(currentPathSegment)) {
            // TODO Why is this? Shouldn't we throw an exception instead?
            LOGGER.trace("Segment {} in path {} is not name item, finishing entity update for delta", currentPathSegment, path);
            break;
        }
        currentItemName = ItemPath.toName(currentPathSegment);
        LOGGER.trace("handleItemDelta: current item name = {}, currentBean = {}, currentBeanType = {}", currentItemName, currentBean, currentBeanType.getJavaType());
        if (currentBean instanceof RAssignment) {
            if (handleAssignment(currentItemName)) {
                break;
            }
        }
        Attribute attribute = findAttributeForCurrentState();
        if (attribute == null) {
            // there's no table/column that needs update
            break;
        }
        if (segmentsIterator.hasNext()) {
            stepThroughAttribute(attribute);
        } else {
            updateAttribute(attribute);
            updateNameCopyAttribute(attribute);
        }
    }
}
Also used : RAssignment(com.evolveum.midpoint.repo.sql.data.common.container.RAssignment) Attribute(javax.persistence.metamodel.Attribute) PrismObject(com.evolveum.midpoint.prism.PrismObject) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject)

Example 30 with Attribute

use of javax.persistence.metamodel.Attribute in project midpoint by Evolveum.

the class EntityRegistry method init.

@PostConstruct
public void init() {
    LOGGER.debug("Starting initialization");
    metamodel = sessionFactory.getMetamodel();
    for (EntityType<?> entity : metamodel.getEntities()) {
        Class<?> javaType = entity.getJavaType();
        Ignore ignore = javaType.getAnnotation(Ignore.class);
        if (ignore != null) {
            continue;
        }
        Class<?> jaxb;
        if (RObject.class.isAssignableFrom(javaType)) {
            // noinspection unchecked,rawtypes
            jaxb = RObjectType.getType((Class<? extends RObject>) javaType).getJaxbClass();
        } else {
            JaxbType jaxbType = javaType.getAnnotation(JaxbType.class);
            if (jaxbType == null) {
                throw new IllegalStateException("Unknown jaxb type for " + javaType.getName());
            }
            jaxb = jaxbType.type();
        }
        jaxbMappings.put(jaxb, entity);
        // create override map
        Map<String, Attribute<?, ?>> overrides = new HashMap<>();
        Map<UniformItemPath, Attribute<?, ?>> pathOverrides = new HashMap<>();
        for (Attribute<?, ?> attribute : entity.getAttributes()) {
            Class<?> jType = attribute.getJavaType();
            JaxbPath[] paths = jType.getAnnotationsByType(JaxbPath.class);
            if (paths == null || paths.length == 0) {
                paths = ((Method) attribute.getJavaMember()).getAnnotationsByType(JaxbPath.class);
            }
            if (paths == null || paths.length == 0) {
                JaxbName name = ((Method) attribute.getJavaMember()).getAnnotation(JaxbName.class);
                if (name != null) {
                    overrides.put(name.localPart(), attribute);
                }
                continue;
            }
            for (JaxbPath path : paths) {
                JaxbName[] names = path.itemPath();
                if (names.length == 1) {
                    overrides.put(names[0].localPart(), attribute);
                } else {
                    UniformItemPath customPath = prismContext.emptyPath();
                    for (JaxbName name : path.itemPath()) {
                        customPath = customPath.append(new QName(name.namespace(), name.localPart()));
                    }
                    pathOverrides.put(customPath, attribute);
                }
            }
        }
        if (!overrides.isEmpty()) {
            attributeNameOverrides.put(entity, overrides);
        }
        if (!pathOverrides.isEmpty()) {
            attributeNamePathOverrides.put(entity, pathOverrides);
        }
    }
    LOGGER.debug("Initialization finished");
}
Also used : Attribute(javax.persistence.metamodel.Attribute) HashMap(java.util.HashMap) JaxbPath(com.evolveum.midpoint.repo.sql.query.definition.JaxbPath) QName(javax.xml.namespace.QName) Method(java.lang.reflect.Method) JaxbName(com.evolveum.midpoint.repo.sql.query.definition.JaxbName) UniformItemPath(com.evolveum.midpoint.prism.path.UniformItemPath) JaxbType(com.evolveum.midpoint.repo.sql.query.definition.JaxbType) PostConstruct(javax.annotation.PostConstruct)

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