Search in sources :

Example 31 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute 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 32 with SingularAttribute

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

the class MetamodelTest method testInheritanceOfVersion.

/**
 * Test for the case with inheritance.
 */
public void testInheritanceOfVersion() {
    Metamodel model = emf.getMetamodel();
    // Check base class which has the version
    try {
        EntityType<?> empType = model.entity(VersionedPerson.class);
        assertNotNull(empType);
        SingularAttribute verAttr = empType.getVersion(long.class);
        assertNotNull(verAttr);
        assertEquals("version", verAttr.getName());
        assertEquals(long.class, verAttr.getJavaType());
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + VersionedPerson.class.getName());
    }
    // Check sub-class which doesn't have the version
    try {
        EntityType<?> empType = model.entity(VersionedEmployee.class);
        assertNotNull(empType);
        SingularAttribute verAttr = empType.getVersion(long.class);
        assertNotNull(verAttr);
        assertEquals("version", verAttr.getName());
        assertEquals(long.class, verAttr.getJavaType());
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + VersionedEmployee.class.getName());
    }
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) VersionedEmployee(org.datanucleus.samples.annotations.versioned.VersionedEmployee) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Metamodel(javax.persistence.metamodel.Metamodel)

Example 33 with SingularAttribute

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

the class MetamodelTest method testGenericIdInheritance.

/**
 * Test for use of a generic id in the base class.
 */
public void testGenericIdInheritance() {
    Metamodel model = emf.getMetamodel();
    try {
        EntityType<GenericIdSub> subType = model.entity(GenericIdSub.class);
        assertNotNull(subType);
        Class idType = subType.getIdType().getJavaType();
        assertEquals(Long.class, idType);
        SingularAttribute idAttr = subType.getId(Long.class);
        assertNotNull(idAttr);
        assertEquals("id", idAttr.getName());
    // TODO Add this check when metamodel supports generic type info
    // assertEquals(Long.class, idAttr.getJavaType());
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + GenericIdSub.class.getName());
    }
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) GenericIdSub(org.datanucleus.samples.annotations.generics.GenericIdSub) Metamodel(javax.persistence.metamodel.Metamodel)

Example 34 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project hibernate-search by hibernate.

the class HibernateSearchPartitionMapper method buildPartitionUnitsFrom.

// Can't do much better without adding generics to EntityTypeDescriptor
@SuppressWarnings("unchecked")
private List<PartitionBound> buildPartitionUnitsFrom(EntityManagerFactory emf, StatelessSession ss, EntityTypeDescriptor entityTypeDescriptor, Integer maxResults, int fetchSize, int rowsPerPartition, IndexScope indexScope) {
    Class<?> javaClass = entityTypeDescriptor.getJavaClass();
    List<PartitionBound> partitionUnits = new ArrayList<>();
    Object lowerID;
    Object upperID = null;
    CriteriaBuilder builder = emf.getCriteriaBuilder();
    CriteriaQuery<?> criteria = builder.createQuery();
    Root<?> root = criteria.from(javaClass);
    entityTypeDescriptor.getIdOrder().addAscOrder(builder, criteria, root);
    EntityType<?> model = root.getModel();
    Class<?> javaType = model.getIdType().getJavaType();
    @SuppressWarnings("rawtypes") SingularAttribute singularAttribute = model.getId(javaType);
    criteria.select(root.get(singularAttribute));
    Query<?> query = ss.createQuery(criteria);
    query.setFetchSize(fetchSize).setReadOnly(true).setCacheable(false).setLockMode(LockModeType.NONE);
    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }
    try (ScrollableResults scroll = query.scroll(ScrollMode.SCROLL_SENSITIVE)) {
        /*
			 * The scroll results are originally positioned *before* the first element,
			 * so we need to scroll rowsPerPartition + 1 positions to advanced to the
			 * upper bound of the first partition, whereas for the next partitions
			 * we only need to advance rowsPerPartition positions.
			 * This handle the special case of the first partition.
			 */
        scroll.next();
        while (scroll.scroll(rowsPerPartition)) {
            lowerID = upperID;
            upperID = scroll.get(0);
            partitionUnits.add(new PartitionBound(javaClass, lowerID, upperID, indexScope));
        }
        // add an additional partition on the tail
        lowerID = upperID;
        upperID = null;
        partitionUnits.add(new PartitionBound(javaClass, lowerID, upperID, indexScope));
        return partitionUnits;
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) SingularAttribute(javax.persistence.metamodel.SingularAttribute) PartitionBound(org.hibernate.search.batch.jsr352.core.massindexing.util.impl.PartitionBound) ScrollableResults(org.hibernate.ScrollableResults)

Example 35 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project igloo-parent by igloo-project.

the class AbstractTestCase method testMetaModel.

/**
 * Méthode utilisée à des fins de tests.
 */
protected void testMetaModel(Attribute<?, ?> attribute, List<Class<?>> classesAutorisees, List<Attribute<?, ?>> ignoredAttributes) throws NoSuchFieldException, SecurityException {
    for (Attribute<?, ?> ignoredAttribute : ignoredAttributes) {
        if (ignoredAttribute.getJavaMember().equals(attribute.getJavaMember())) {
            // champ ignoré
            return;
        }
    }
    Enumerated enumerated = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(Enumerated.class);
    MapKeyEnumerated mapKeyEnumerated = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(MapKeyEnumerated.class);
    MapKey mapKey = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(MapKey.class);
    // cas des embeddable et des collectionOfElements d'embeddable
    if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.ELEMENT_COLLECTION) && EmbeddableTypeImpl.class.isInstance(((PluralAttribute<?, ?, ?>) attribute).getElementType())) {
        PluralAttribute<?, ?, ?> pluralAttribute = (PluralAttribute<?, ?, ?>) attribute;
        if (classesAutorisees.contains(pluralAttribute.getElementType().getJavaType())) {
            // type autorisé de manière explicite
            return;
        }
        for (Attribute<?, ?> embeddedAttribute : ((EmbeddableTypeImpl<?>) pluralAttribute.getElementType()).getAttributes()) {
            testMetaModel(embeddedAttribute, classesAutorisees, ignoredAttributes);
        }
        return;
    } else if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.EMBEDDED)) {
        SingularAttribute<?, ?> singularAttribute = (SingularAttribute<?, ?>) attribute;
        if (classesAutorisees.contains(singularAttribute.getJavaType())) {
            // type autorisé de manière explicite
            return;
        }
        if (EmbeddableTypeImpl.class.isInstance(singularAttribute.getType())) {
            for (Attribute<?, ?> embeddedAttribute : ((EmbeddableTypeImpl<?>) singularAttribute.getType()).getAttributes()) {
                testMetaModel(embeddedAttribute, classesAutorisees, ignoredAttributes);
            }
            return;
        }
    }
    if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.BASIC) && !classesAutorisees.contains(attribute.getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
        throw new IllegalStateException("Champ \"" + attribute.getName() + "\", de type " + attribute.getJavaType().getSimpleName() + " refusé");
    } else if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.ELEMENT_COLLECTION) && PluralAttribute.class.isInstance(attribute) && !classesAutorisees.contains(((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
        PluralAttribute<?, ?, ?> pluralAttribute = (PluralAttribute<?, ?, ?>) attribute;
        throw new IllegalStateException("Collection \"" + attribute.getName() + "\" de " + pluralAttribute.getElementType().getJavaType().getSimpleName() + " refusée");
    } else if (attribute instanceof MapAttribute) {
        MapAttribute<?, ?, ?> mapAttribute = (MapAttribute<?, ?, ?>) attribute;
        if (Enum.class.isAssignableFrom(mapAttribute.getKeyJavaType()) && (mapKeyEnumerated == null || EnumType.ORDINAL.equals(mapKeyEnumerated.value())) && mapKey == null) /* if @MapKey present, then field format is defined elsewhere and check is useless */
        {
            throw new IllegalStateException("Map \"" + attribute.getName() + "\" de clés ordinales " + ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType().getSimpleName() + " refusée");
        }
        if (Enum.class.isAssignableFrom(mapAttribute.getElementType().getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
            throw new IllegalStateException("Map \"" + attribute.getName() + "\" de valeurs ordinales " + ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType().getSimpleName() + " refusée");
        }
    }
}
Also used : EmbeddableTypeImpl(org.hibernate.metamodel.model.domain.internal.EmbeddableTypeImpl) PluralAttribute(javax.persistence.metamodel.PluralAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) MapKeyEnumerated(javax.persistence.MapKeyEnumerated) Enumerated(javax.persistence.Enumerated) MapKeyEnumerated(javax.persistence.MapKeyEnumerated) SingularAttribute(javax.persistence.metamodel.SingularAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) MapKey(javax.persistence.MapKey)

Aggregations

SingularAttribute (javax.persistence.metamodel.SingularAttribute)38 Attribute (javax.persistence.metamodel.Attribute)13 Metamodel (javax.persistence.metamodel.Metamodel)13 ListAttribute (javax.persistence.metamodel.ListAttribute)8 ArrayList (java.util.ArrayList)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)7 MapAttribute (javax.persistence.metamodel.MapAttribute)7 Test (org.junit.Test)7 Predicate (javax.persistence.criteria.Predicate)5 EntityType (javax.persistence.metamodel.EntityType)5 Root (javax.persistence.criteria.Root)4 PluralAttribute (javax.persistence.metamodel.PluralAttribute)4 EntityManager (javax.persistence.EntityManager)3 Path (javax.persistence.criteria.Path)3 IdentifiableType (javax.persistence.metamodel.IdentifiableType)3 Type (javax.persistence.metamodel.Type)3 VersionedPerson (org.datanucleus.samples.annotations.versioned.VersionedPerson)3 EntityTransaction (javax.persistence.EntityTransaction)2 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2