Search in sources :

Example 16 with SingularAttribute

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

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

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

use of javax.persistence.metamodel.SingularAttribute in project deltaspike by apache.

the class EntityRepositoryHandlerTest method should_count_with_no_attributes.

@Test
@SuppressWarnings("unchecked")
public void should_count_with_no_attributes() {
    // given
    Simple simple = testData.createSimple("testFindAll1");
    testData.createSimple("testFindAll2");
    SingularAttribute<Simple, Object>[] attributes = new SingularAttribute[] {};
    // when
    Long result = repo.count(simple, attributes);
    // then
    assertEquals(Long.valueOf(2), result);
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) Simple(org.apache.deltaspike.data.test.domain.Simple) Test(org.junit.Test)

Example 20 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project CzechIdMng by bcvsolutions.

the class DefaultIdmAutomaticRoleAttributeService method getPredicateForRuleByContract.

/**
 * Return predicate for given rule by contract
 *
 * @param rule
 * @param root
 * @param query
 * @param cb
 * @return
 */
private Predicate getPredicateForRuleByContract(IdmAutomaticRoleAttributeRuleDto rule, Root<IdmIdentityContract> root, CriteriaQuery<?> query, CriteriaBuilder cb, boolean pass) {
    // 
    Metamodel metamodel = entityManager.getMetamodel();
    if (rule.getType() == AutomaticRoleAttributeRuleType.CONTRACT) {
        SingularAttribute<? super IdmIdentityContract, ?> singularAttribute = metamodel.entity(IdmIdentityContract.class).getSingularAttribute(rule.getAttributeName());
        Path<Object> path = root.get(singularAttribute.getName());
        // role will be added and wich roles will be removed.
        return getPredicateWithComparsion(path, castToType(singularAttribute, rule.getValue(), rule.getComparison()), cb, rule.getComparison(), !pass);
    } else if (rule.getType() == AutomaticRoleAttributeRuleType.CONTRACT_EAV) {
        IdmFormAttributeDto formAttributeDto = formAttributeService.get(rule.getFormAttribute());
        AutomaticRoleAttributeRuleComparison comparison = rule.getComparison();
        // Cast given value to specific persistent type
        // For is empty and is not empty comparison is returned null even if value exists
        Object value = getFormValue(rule.getValue(), formAttributeDto, comparison);
        // 
        // For contract form attribute was composed only one subquery
        Subquery<IdmIdentityContractFormValue> subquery = query.subquery(IdmIdentityContractFormValue.class);
        Root<IdmIdentityContractFormValue> subRoot = subquery.from(IdmIdentityContractFormValue.class);
        subquery.select(subRoot);
        // 
        Path<?> path = subRoot.get(getSingularAttributeForEav(formAttributeDto.getPersistentType()));
        // Is empty comparison has specific behavior because form value isn't empty, but value doesn't exist
        if (comparison == AutomaticRoleAttributeRuleComparison.IS_EMPTY) {
            subquery.where(cb.or(// Predicate for check if value exists
            getPredicateForNullFormAttributeIdentityContract(root, query, cb, formAttributeDto), cb.and(cb.equal(subRoot.get(IdmIdentityContractFormValue_.owner), root), cb.equal(subRoot.get(IdmIdentityContractFormValue_.formAttribute).get(AbstractFormValue_.id), formAttributeDto.getId()), getPredicateWithComparsion(path, null, cb, rule.getComparison(), null))));
            if (pass) {
                return cb.not(cb.exists(subquery));
            }
            return cb.exists(subquery);
        }
        // 
        subquery.where(cb.and(cb.equal(subRoot.get(IdmIdentityContractFormValue_.owner), root), cb.equal(subRoot.get(IdmIdentityContractFormValue_.formAttribute).get(AbstractFormValue_.id), formAttributeDto.getId()), getPredicateWithComparsion(path, value, cb, rule.getComparison(), null)));
        // 
        Predicate existsInEav = getPredicateForConnection(subquery, cb, pass, formAttributeDto.isMultiple());
        // For comparison with not is required also check null values
        if (comparison == AutomaticRoleAttributeRuleComparison.NOT_CONTAINS || comparison == AutomaticRoleAttributeRuleComparison.NOT_END_WITH || comparison == AutomaticRoleAttributeRuleComparison.NOT_EQUALS || comparison == AutomaticRoleAttributeRuleComparison.NOT_START_WITH) {
            if (pass) {
                existsInEav = cb.or(existsInEav, cb.not(getPredicateForNullFormAttributeIdentityContract(root, query, cb, formAttributeDto)));
            } else {
                existsInEav = cb.and(existsInEav, getPredicateForNullFormAttributeIdentityContract(root, query, cb, formAttributeDto));
            }
        }
        // 
        return existsInEav;
    } else if (rule.getType() == AutomaticRoleAttributeRuleType.IDENTITY_EAV) {
        IdmFormAttributeDto formAttributeDto = formAttributeService.get(rule.getFormAttribute());
        AutomaticRoleAttributeRuleComparison comparison = rule.getComparison();
        // Cast given value to specific persistent type
        // For is empty and is not empty comparison is returned null even if value exists
        Object value = getFormValue(rule.getValue(), formAttributeDto, comparison);
        // 
        // Rules for identity form values must contains two subquery identity -> identity eav
        Subquery<IdmIdentity> subquery = query.subquery(IdmIdentity.class);
        Root<IdmIdentity> subRoot = subquery.from(IdmIdentity.class);
        subquery.select(subRoot);
        // 
        Subquery<IdmIdentityFormValue> subQueryIdentityEav = query.subquery(IdmIdentityFormValue.class);
        Root<IdmIdentityFormValue> subRootIdentityEav = subQueryIdentityEav.from(IdmIdentityFormValue.class);
        subQueryIdentityEav.select(subRootIdentityEav);
        // 
        Path<?> path = subRootIdentityEav.get(getSingularAttributeForEav(formAttributeDto.getPersistentType()));
        // Is empty comparison has specific behavior because form value isn't empty, but value doesn't exist
        if (comparison == AutomaticRoleAttributeRuleComparison.IS_EMPTY) {
            subquery.where(cb.and(cb.equal(root.get(IdmIdentityContract_.identity), subRoot), cb.or(cb.exists(subQueryIdentityEav.where(cb.and(cb.equal(subRootIdentityEav.get(IdmIdentityFormValue_.owner), subRoot), cb.equal(subRootIdentityEav.get(IdmIdentityFormValue_.formAttribute).get(AbstractFormValue_.id), formAttributeDto.getId()), getPredicateWithComparsion(path, null, cb, rule.getComparison(), null)))), // Predicate for check if value exists
            getPredicateForNullFormAttributeIdentity(subRoot, subquery, cb, formAttributeDto))));
            // 
            if (pass) {
                return cb.not(cb.exists(subquery));
            }
            return cb.exists(subquery);
        }
        // 
        subQueryIdentityEav.where(cb.and(cb.equal(subRootIdentityEav.get(IdmIdentityFormValue_.owner), subRoot), cb.equal(root.get(IdmIdentityContract_.identity), subRoot), cb.equal(subRootIdentityEav.get(IdmIdentityFormValue_.formAttribute).get(AbstractFormValue_.id), formAttributeDto.getId()), getPredicateWithComparsion(path, value, cb, rule.getComparison(), null)));
        Predicate existsInEav = getPredicateForConnection(subQueryIdentityEav, cb, pass, formAttributeDto.isMultiple());
        // For comparison with not is required also check null values
        if (comparison == AutomaticRoleAttributeRuleComparison.NOT_CONTAINS || comparison == AutomaticRoleAttributeRuleComparison.NOT_END_WITH || comparison == AutomaticRoleAttributeRuleComparison.NOT_EQUALS || comparison == AutomaticRoleAttributeRuleComparison.NOT_START_WITH) {
            if (pass) {
                existsInEav = cb.or(existsInEav, cb.not(getPredicateForNullFormAttributeIdentity(subRoot, subquery, cb, formAttributeDto)));
            } else {
                existsInEav = cb.and(existsInEav, getPredicateForNullFormAttributeIdentity(subRoot, subquery, cb, formAttributeDto));
            }
        }
        // 
        subquery.where(cb.and(cb.equal(subRoot.get(IdmIdentity_.id), root.get(IdmIdentityContract_.identity).get(AbstractEntity_.id)), existsInEav));
        // 
        return cb.exists(subquery);
    } else if (rule.getType() == AutomaticRoleAttributeRuleType.IDENTITY) {
        Subquery<IdmIdentity> subquery = query.subquery(IdmIdentity.class);
        Root<IdmIdentity> subRoot = subquery.from(IdmIdentity.class);
        subquery.select(subRoot);
        // 
        SingularAttribute<? super IdmIdentity, ?> singularAttribute = metamodel.entity(IdmIdentity.class).getSingularAttribute(rule.getAttributeName());
        Path<Object> path = subRoot.get(singularAttribute.getName());
        // 
        subquery.where(// correlation attr
        cb.and(// correlation attr
        cb.equal(subRoot.get(IdmIdentity_.id), root.get(IdmIdentityContract_.identity).get(AbstractEntity_.id)), getPredicateWithComparsion(path, castToType(singularAttribute, rule.getValue(), rule.getComparison()), cb, rule.getComparison(), null)));
        // 
        return getPredicateForConnection(subquery, cb, pass, false);
    } else {
        throw new UnsupportedOperationException("Type: " + rule.getType().name() + ", isn't supported for contract rules!");
    }
}
Also used : Path(javax.persistence.criteria.Path) AutomaticRoleAttributeRuleComparison(eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison) Root(javax.persistence.criteria.Root) IdmIdentityFormValue(eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue) Subquery(javax.persistence.criteria.Subquery) Predicate(javax.persistence.criteria.Predicate) SingularAttribute(javax.persistence.metamodel.SingularAttribute) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) Metamodel(javax.persistence.metamodel.Metamodel) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) IdmIdentityContract(eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract) IdmIdentityContractFormValue(eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityContractFormValue)

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