Search in sources :

Example 31 with Attribute

use of javax.persistence.metamodel.Attribute in project jo-client-platform by jo-source.

the class CriteriaQueryCreator method getJoinQueryPath.

private Path<?> getJoinQueryPath(final Root<?> bean, final String propertyName) {
    final QueryPath queryPath = getQueryPathAnno(bean, propertyName);
    if (queryPath != null) {
        Path<?> parentPath = bean;
        Type<?> type = bean.getModel();
        for (final String pathSegment : queryPath.path()) {
            final Attribute<?, ?> attribute;
            if (type != null && type instanceof ManagedType) {
                attribute = ((ManagedType<?>) type).getAttribute(pathSegment);
                type = getType(attribute);
            } else {
                attribute = null;
            }
            if (isJoinableType(attribute) && parentPath instanceof From) {
                final From<?, ?> from = (From<?, ?>) parentPath;
                parentPath = from.join(pathSegment, JoinType.LEFT);
            } else {
                parentPath = parentPath.get(pathSegment);
            }
        }
        return parentPath;
    } else if (IPropertyMap.class.isAssignableFrom(bean.getJavaType())) {
        final PropertyMapQueryPath propertyMapQueryPath = bean.getJavaType().getAnnotation(PropertyMapQueryPath.class);
        if (propertyMapQueryPath != null) {
            Path<?> parentPath = bean;
            Type<?> type = bean.getModel();
            for (final String pathSegment : propertyMapQueryPath.path()) {
                final Attribute<?, ?> attribute;
                if (type != null && type instanceof ManagedType) {
                    attribute = ((ManagedType<?>) type).getAttribute(pathSegment);
                    type = getType(attribute);
                } else {
                    attribute = null;
                }
                if (isJoinableType(attribute) && parentPath instanceof From) {
                    final From<?, ?> from = (From<?, ?>) parentPath;
                    parentPath = from.join(pathSegment, JoinType.LEFT);
                } else {
                    parentPath = parentPath.get(pathSegment);
                }
            }
            parentPath.alias(PROPERTY_MAP_JOIN_ALIAS);
            return parentPath.get(propertyMapQueryPath.valuePath());
        }
    }
    return null;
}
Also used : PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath) QueryPath(org.jowidgets.cap.service.jpa.api.query.QueryPath) Path(javax.persistence.criteria.Path) ManagedType(javax.persistence.metamodel.ManagedType) IPropertyMap(org.jowidgets.cap.common.api.bean.IPropertyMap) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) From(javax.persistence.criteria.From) PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath) PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath) QueryPath(org.jowidgets.cap.service.jpa.api.query.QueryPath) JoinType(javax.persistence.criteria.JoinType) ManagedType(javax.persistence.metamodel.ManagedType) Type(javax.persistence.metamodel.Type)

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