Example usage for javax.persistence.metamodel PluralAttribute getElementType

List of usage examples for javax.persistence.metamodel PluralAttribute getElementType

Introduction

In this page you can find the example usage for javax.persistence.metamodel PluralAttribute getElementType.

Prototype

Type<E> getElementType();

Source Link

Document

Return the type representing the element type of the collection.

Usage

From source file:de.micromata.genome.jpa.metainf.MetaInfoUtils.java

/**
 * Gets the column meta data.//w w  w .j  a v a2s. c  o m
 *
 * @param entity the entity
 * @param entityClass the entity class
 * @param at the at
 * @param pdo the pdo
 * @return the column meta data
 */
public static ColumnMetadata getColumnMetaData(EntityMetadataBean entity, Class<?> entityClass,
        Attribute<?, ?> at, Optional<PropertyDescriptor> pdo) {
    ColumnMetadataBean ret = new ColumnMetadataBean(entity);
    ret.setName(at.getName());
    ret.setJavaType(at.getJavaType());
    ret.setAssociation(at.isAssociation());
    ret.setCollection(at.isCollection());

    if (at instanceof PluralAttribute) {
        PluralAttribute pa = (PluralAttribute) at;
        Type eltype = pa.getElementType();
        CollectionType coltype = pa.getCollectionType();
    }

    Member jm = at.getJavaMember();
    // TODO maybe handle this.
    at.getPersistentAttributeType();
    if ((jm instanceof AccessibleObject) == false) {
        LOG.warn("Column " + at.getName() + " ha no valid Java Member");
        return ret;
    }
    AccessibleObject ao = (AccessibleObject) jm;
    getGetterSetter(entityClass, ao, pdo, ret);
    ret.setAnnotations(getFieldAndMemberAnnots(entityClass, ao));
    if (ret.getAnnotations().stream().filter((anot) -> anot.getClass() == Transient.class).count() > 0) {
        return null;
    }
    Column colc = ao.getAnnotation(Column.class);
    if (colc == null) {
        ret.setMaxLength(255);
        return ret;
    }
    String name = colc.name();
    if (StringUtils.isEmpty(name) == true) {
        ret.setDatabaseName(ret.getName());
    } else {
        ret.setDatabaseName(name);
    }
    ret.setMaxLength(colc.length());
    ret.setUnique(colc.unique());
    ret.setColumnDefinition(colc.columnDefinition());
    ret.setInsertable(colc.insertable());
    ret.setNullable(colc.nullable());
    ret.setPrecision(colc.precision());
    ret.setScale(colc.scale());
    return ret;
}

From source file:org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl.java

/**
 * {@inheritDoc}/*from  w ww .  j  ava 2 s.  c  o m*/
 */
@Override
protected void populateImplementationSpecificCollectionLevelMetadata(DataObjectCollectionImpl collection,
        PluralAttribute<?, ?, ?> cd) {
    // OJB stores the related class object name. We need to go into the repository and grab the table name.
    Class<?> collectionElementClass = cd.getElementType().getJavaType();
    EntityType<?> elementEntityType = entityManager.getMetamodel().entity(collectionElementClass);
    // get table name behind element
    if (elementEntityType instanceof EntityTypeImpl) {
        collection.setBackingObjectName(((EntityTypeImpl<?>) elementEntityType).getDescriptor().getTableName());
    }

    // Set to read only if store (save) operations should not be pushed through
    PersistentAttributeType persistentAttributeType = cd.getPersistentAttributeType();

    if (cd instanceof PluralAttributeImpl) {
        PluralAttributeImpl<?, ?, ?> coll = (PluralAttributeImpl<?, ?, ?>) cd;
        CollectionMapping collectionMapping = coll.getCollectionMapping();

        if (collectionMapping instanceof OneToManyMapping) {
            OneToManyMapping otm = (OneToManyMapping) collectionMapping;
            populateInverseRelationship(otm, collection);
            Map<DatabaseField, DatabaseField> keyMap = otm.getSourceKeysToTargetForeignKeys();
            List<DataObjectAttributeRelationship> attributeRelationships = new ArrayList<DataObjectAttributeRelationship>();
            for (Map.Entry<DatabaseField, DatabaseField> keyRel : keyMap.entrySet()) {
                attributeRelationships.add(new DataObjectAttributeRelationshipImpl(
                        getPropertyNameFromDatabaseColumnName(cd.getDeclaringType(), keyRel.getKey().getName()),
                        getPropertyNameFromDatabaseColumnName(elementEntityType, keyRel.getValue().getName())));
            }
            collection.setAttributeRelationships(attributeRelationships);
        }

        collection.setReadOnly(collectionMapping.isReadOnly());
        collection.setSavedWithParent(collectionMapping.isCascadePersist());
        collection.setDeletedWithParent(collectionMapping.isCascadeRemove());
        collection
                .setLoadedAtParentLoadTime(collectionMapping.isCascadeRefresh() && !collectionMapping.isLazy());
        collection.setLoadedDynamicallyUponUse(
                collectionMapping.isCascadeRefresh() && collectionMapping.isLazy());
    } else {
        // get what we can based on JPA values (note that we just set some to have values here)
        collection.setReadOnly(false);
        collection.setSavedWithParent(persistentAttributeType == PersistentAttributeType.ONE_TO_MANY);
        collection.setDeletedWithParent(persistentAttributeType == PersistentAttributeType.ONE_TO_MANY);
        collection.setLoadedAtParentLoadTime(true);
        collection.setLoadedDynamicallyUponUse(false);
    }

    // We need to detect the case of a intermediate mapping table. These tables are not directly mapped
    // in OJB, but are referenced by their table and column names.
    // The attributes referenced are assumed to be in the order of the PK fields of the parent and child objects
    // as there is no way to identify the attributes/columns on the linked classes.

    // Extract the default sort order for the collection
    List<DataObjectCollectionSortAttribute> sortAttributes = new ArrayList<DataObjectCollectionSortAttribute>();
    if (cd instanceof PluralAttributeImpl) {
        PluralAttributeImpl<?, ?, ?> coll = (PluralAttributeImpl<?, ?, ?>) cd;
        CollectionMapping collectionMapping = coll.getCollectionMapping();
        if (collectionMapping.getSelectionQuery() instanceof ObjectLevelReadQuery) {
            ObjectLevelReadQuery readQuery = (ObjectLevelReadQuery) collectionMapping.getSelectionQuery();
            List<Expression> orderByExpressions = readQuery.getOrderByExpressions();
            for (Expression expression : orderByExpressions) {
                if (expression instanceof FunctionExpression) {
                    String attributeName = ((FunctionExpression) expression).getBaseExpression().getName();
                    SortDirection direction = SortDirection.ASCENDING;
                    if (expression.getOperator().isOrderOperator()) {
                        if (StringUtils.containsIgnoreCase(expression.getOperator().getDatabaseStrings()[0],
                                "DESC")) {
                            direction = SortDirection.DESCENDING;
                        }
                    }
                    sortAttributes.add(new DataObjectCollectionSortAttributeImpl(attributeName, direction));
                }
            }
        }

    }
    collection.setDefaultCollectionOrderingAttributeNames(sortAttributes);
}