Example usage for javax.persistence.metamodel ManagedType getPluralAttributes

List of usage examples for javax.persistence.metamodel ManagedType getPluralAttributes

Introduction

In this page you can find the example usage for javax.persistence.metamodel ManagedType getPluralAttributes.

Prototype

Set<PluralAttribute<? super X, ?, ?>> getPluralAttributes();

Source Link

Document

Return all multi-valued attributes (Collection-, Set-, List-, and Map-valued attributes) of the managed type.

Usage

From source file:com.dbs.sdwt.jpa.ByExampleUtil.java

public <T> List<Predicate> byExampleOnXToMany(ManagedType<T> mt, Root<T> mtPath, T mtValue, SearchParameters sp,
        CriteriaBuilder builder) {/*  w w w .ja v  a  2s. c o  m*/
    List<Predicate> predicates = newArrayList();
    for (PluralAttribute<? super T, ?, ?> pa : mt.getPluralAttributes()) {
        if (pa.getCollectionType() == PluralAttribute.CollectionType.LIST) {
            List<?> values = (List<?>) jpaUtil.getValue(mtValue, mt.getAttribute(pa.getName()));
            if (values != null && !values.isEmpty()) {
                if (sp.getUseAndInXToMany()) {
                    if (values.size() > 3) {
                        log.warn(
                                "Please note that using AND restriction on an Many to Many relationship requires as many joins as values");
                    }
                    for (Object value : values) {
                        ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName()));
                        predicates.add(join.in(value));
                    }
                } else {
                    ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName()));
                    predicates.add(join.in(values));
                }
            }
        }
    }
    return predicates;
}

From source file:org.querybyexample.jpa.ByExampleUtil.java

/**
 * Construct a join predicate on collection (eg many to many, List)
 *//*from  ww w  . j  a v  a  2s . c om*/
public <T> List<Predicate> byExampleOnManyToMany(ManagedType<T> mt, Root<T> mtPath, T mtValue,
        SearchParameters sp, CriteriaBuilder builder) {
    List<Predicate> predicates = newArrayList();
    for (PluralAttribute<? super T, ?, ?> pa : mt.getPluralAttributes()) {
        if (pa.getCollectionType() == PluralAttribute.CollectionType.LIST) {
            List<?> values = (List<?>) JpaUtil.getValue(mtValue, mt.getAttribute(pa.getName()));
            if (values != null && !values.isEmpty()) {
                if (sp.getUseANDInManyToMany()) {
                    if (values.size() > 3) {
                        log.warn(
                                "Please note that using AND restriction on an Many to Many relationship requires as many joins as values");
                    }
                    for (Object value : values) {
                        ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName()));
                        predicates.add(join.in(value));
                    }
                } else {
                    ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName()));
                    predicates.add(join.in(values));
                }
            }
        }
    }
    return predicates;
}