Example usage for javax.persistence.metamodel EntityType getCollection

List of usage examples for javax.persistence.metamodel EntityType getCollection

Introduction

In this page you can find the example usage for javax.persistence.metamodel EntityType getCollection.

Prototype

CollectionAttribute<? super X, ?> getCollection(String name);

Source Link

Document

Return the Collection-valued attribute of the managed type that corresponds to the specified name.

Usage

From source file:ru.savvy.jpafilterbuilder.FilterCriteriaBuilder.java

/**
 * This clumsy code is just to get the class of plural attribute mapping
 *
 * @param et//from   w  w w  .  j  a v a 2 s . c om
 * @param fieldName
 * @return
 */
private Class<?> getPluralJavaType(EntityType<?> et, String fieldName) {
    for (PluralAttribute pa : et.getPluralAttributes()) {
        if (pa.getName().equals(fieldName)) {
            switch (pa.getCollectionType()) {
            case COLLECTION:
                return et.getCollection(fieldName).getElementType().getJavaType();
            case LIST:
                return et.getList(fieldName).getElementType().getJavaType();
            case SET:
                return et.getSet(fieldName).getElementType().getJavaType();
            case MAP:
                throw new UnsupportedOperationException("Entity Map mapping unsupported for entity: "
                        + et.getName() + " field name: " + fieldName);
            }
        }
    }
    throw new IllegalArgumentException(
            "Field " + fieldName + " of entity " + et.getName() + " is not a plural attribute");
}