Example usage for javax.persistence.metamodel Attribute isAssociation

List of usage examples for javax.persistence.metamodel Attribute isAssociation

Introduction

In this page you can find the example usage for javax.persistence.metamodel Attribute isAssociation.

Prototype

boolean isAssociation();

Source Link

Document

Is the attribute an association.

Usage

From source file:com.impetus.client.neo4j.GraphEntityMapper.java

/**
 * /*from   w  ww  .j  a  v  a  2  s.com*/
 * Converts a {@link Relationship} object to corresponding entity object
 */
public Object getEntityFromRelationship(Relationship relationship, EntityMetadata topLevelEntityMetadata,
        Relation relation) {
    EntityMetadata relationshipEntityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
            relation.getMapKeyJoinClass());

    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(relationshipEntityMetadata.getPersistenceUnit());
    EntityType entityType = metaModel.entity(relationshipEntityMetadata.getEntityClazz());

    // Iterate over, entity attributes
    Set<Attribute> attributes = entityType.getSingularAttributes();

    Object entity = null;

    try {
        // entity =
        // relationshipEntityMetadata.getEntityClazz().newInstance();

        for (Attribute attribute : attributes) {
            Field field = (Field) attribute.getJavaMember();
            String columnName = ((AbstractAttribute) attribute).getJPAColumnName();

            // Set Entity level properties
            if (metaModel.isEmbeddable(relationshipEntityMetadata.getIdAttribute().getBindableJavaType())
                    && relationshipEntityMetadata.getIdAttribute().getJavaType().equals(field.getType())) {
                Object idValue = deserializeIdAttributeValue(relationshipEntityMetadata,
                        (String) relationship.getProperty(columnName));
                if (idValue != null) {
                    entity = initialize(relationshipEntityMetadata, entity);
                    PropertyAccessorHelper.set(entity, field, idValue);
                }
            } else if (!attribute.isCollection() && !attribute.isAssociation()
                    && !field.getType().equals(topLevelEntityMetadata.getEntityClazz())
                    && !field.getType().equals(relation.getTargetEntity())) {
                Object value = relationship.getProperty(columnName, null);
                if (value != null) {
                    entity = initialize(relationshipEntityMetadata, entity);
                    PropertyAccessorHelper.set(entity, field, fromNeo4JObject(value, field));
                }
            }
        }

        if (entity != null
                && !metaModel.isEmbeddable(relationshipEntityMetadata.getIdAttribute().getBindableJavaType())) {
            Object rowKey = relationship.getProperty(
                    ((AbstractAttribute) relationshipEntityMetadata.getIdAttribute()).getJPAColumnName());
            if (rowKey != null) {
                PropertyAccessorHelper.setId(entity, relationshipEntityMetadata, fromNeo4JObject(rowKey,
                        (Field) relationshipEntityMetadata.getIdAttribute().getJavaMember()));
            }
        }
    } catch (NotFoundException e) {
        log.info(e.getMessage());
        return null;
    }

    return entity;
}

From source file:com.impetus.client.redis.RedisClient.java

/**
 * Wraps entity attributes into byte[] and return instance of attribute
 * wrapper.//from   w  w w  . ja va  2 s  .  c  o  m
 * 
 * @param entityMetadata
 * @param entity
 * @return
 */
private AttributeWrapper wrap(EntityMetadata entityMetadata, Object entity) {
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());

    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());
    Set<Attribute> attributes = entityType.getAttributes();

    // attributes can be null??? i guess NO
    AttributeWrapper wrapper = new AttributeWrapper(attributes.size());

    List<String> relationNames = entityMetadata.getRelationNames();

    // PropertyAccessorHelper.get(entity,
    for (Attribute attr : attributes) {
        if (/* !entityMetadata.getIdAttribute().equals(attr) && */!attr.isAssociation()) {
            if (metaModel.isEmbeddable(((AbstractAttribute) attr).getBindableJavaType())) {
                EmbeddableType embeddableAttribute = metaModel
                        .embeddable(((AbstractAttribute) attr).getBindableJavaType());

                Object embeddedObject = PropertyAccessorHelper.getObject(entity, (Field) attr.getJavaMember());

                Set<Attribute> embeddedAttributes = embeddableAttribute.getAttributes();

                for (Attribute attrib : embeddedAttributes) {
                    addToWrapper(entityMetadata, wrapper, embeddedObject, attrib, attr);
                }

            } else {
                addToWrapper(entityMetadata, wrapper, entity, attr);
            }
        } else if (attributes.size() == 1) // means it is only a key! weird
                                           // but possible negative
                                           // scenario
        {
            byte[] value = PropertyAccessorHelper.get(entity, (Field) attr.getJavaMember());
            byte[] name;
            name = getEncodedBytes(((AbstractAttribute) attr).getJPAColumnName());

            // add column name as key and value as value
            wrapper.addColumn(name, value);

        }
    }

    return wrapper;
}

From source file:com.impetus.client.cassandra.CassandraClientBase.java

/**
 * Return update query string for given entity intended for counter column
 * family./*w  w  w  .  j a v a  2s .c  o m*/
 * 
 * @param entityMetadata
 *            the entity metadata
 * @param entity
 *            the entity
 * @param cassandra_client
 *            the cassandra_client
 * @param rlHolders
 *            the rl holders
 * @return the list
 */
protected List<String> createUpdateQueryForCounter(EntityMetadata entityMetadata, Object entity,
        Cassandra.Client cassandra_client, List<RelationHolder> rlHolders) {
    Map<String, String> builders = new HashMap<String, String>();

    CQLTranslator translator = new CQLTranslator();

    Object rowId = PropertyAccessorHelper.getId(entity, entityMetadata);
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());

    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());

    Set<Attribute> attributes = entityType.getAttributes();

    for (Attribute attrib : attributes) {
        if (!entityMetadata.getIdAttribute().getName().equals(attrib.getName())
                && !metaModel.isEmbeddable(attrib.getJavaType()) && !attrib.isAssociation()) {
            String tableName = ((AbstractAttribute) attrib).getTableName() != null
                    ? ((AbstractAttribute) attrib).getTableName()
                    : entityMetadata.getTableName();

            String queryString = builders.get(tableName);
            StringBuilder builder;
            if (queryString == null) {
                builder = new StringBuilder();
            } else {
                builder = new StringBuilder(queryString);
            }
            translator.buildSetClauseForCounters(builder, ((AbstractAttribute) attrib).getJPAColumnName(),
                    PropertyAccessorHelper.getObject(entity, attrib.getName()));
            builders.put(tableName, builder.toString());
        }
    }
    for (RelationHolder rl : rlHolders) {
        translator.buildSetClauseForCounters(new StringBuilder(builders.get(entityMetadata.getTableName())),
                rl.getRelationName(), rl.getRelationValue());
    }

    for (String tableName : builders.keySet()) {
        StringBuilder builder = new StringBuilder(builders.get(tableName));

        String update_Query = translator.UPDATE_QUERY;

        update_Query = StringUtils.replace(update_Query, CQLTranslator.COLUMN_FAMILY,
                translator.ensureCase(new StringBuilder(), tableName, false).toString());

        // strip last "," clause.
        builder.delete(builder.lastIndexOf(CQLTranslator.COMMA_STR), builder.length());

        builder.append(CQLTranslator.ADD_WHERE_CLAUSE);
        onWhereClause(entityMetadata, rowId, translator, builder, metaModel, entityMetadata.getIdAttribute());

        // strip last "AND" clause.
        builder.delete(builder.lastIndexOf(CQLTranslator.AND_CLAUSE), builder.length());

        StringBuilder queryBuilder = new StringBuilder(update_Query);
        queryBuilder.append(CQLTranslator.ADD_SET_CLAUSE);
        queryBuilder.append(builder);

        if (log.isDebugEnabled()) {
            log.debug("Returning update query {}.", queryBuilder.toString());
        }

        builders.put(tableName, queryBuilder.toString());
    }
    return new ArrayList(builders.values());
}

From source file:com.impetus.client.oraclenosql.OracleNoSQLClient.java

/**
 * Scroll and populate./*from   w ww.ja  v a  2s.  c  om*/
 * 
 * @param key
 *            the key
 * @param entityMetadata
 *            the entity metadata
 * @param metaModel
 *            the meta model
 * @param schemaTable
 *            the schema table
 * @param rowsIter
 *            the rows iter
 * @param relationMap
 *            the relation map
 * @param columnsToSelect
 *            the columns to select
 * @return the list
 * @throws InstantiationException
 *             the instantiation exception
 * @throws IllegalAccessException
 *             the illegal access exception
 */
private List scrollAndPopulate(Object key, EntityMetadata entityMetadata, MetamodelImpl metaModel,
        Table schemaTable, Iterator<Row> rowsIter, Map<String, Object> relationMap,
        List<String> columnsToSelect) throws InstantiationException, IllegalAccessException {
    List results = new ArrayList();
    Object entity = null;
    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());
    // here
    while (rowsIter.hasNext()) {
        relationMap = new HashMap<String, Object>();
        entity = initializeEntity(key, entityMetadata);

        Row row = rowsIter.next();

        List<String> fields = row.getTable().getFields();
        FieldDef fieldMetadata = null;
        FieldValue value = null;
        String idColumnName = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName();
        if (/* eligibleToFetch(columnsToSelect, idColumnName) && */!metaModel
                .isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) {
            populateId(entityMetadata, schemaTable, entity, row);
        } else {
            onEmbeddableId(entityMetadata, metaModel, schemaTable, entity, row);
        }

        Iterator<String> fieldIter = fields.iterator();

        Set<Attribute> attributes = entityType.getAttributes();
        for (Attribute attribute : attributes) {
            String jpaColumnName = ((AbstractAttribute) attribute).getJPAColumnName();
            if (eligibleToFetch(columnsToSelect, jpaColumnName)
                    && !attribute.getName().equals(entityMetadata.getIdAttribute().getName())) {
                if (metaModel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
                    // readEmbeddable(value, columnsToSelect,
                    // entityMetadata, metaModel, schemaTable, value,
                    // attribute);
                    EmbeddableType embeddableId = metaModel
                            .embeddable(((AbstractAttribute) attribute).getBindableJavaType());
                    Set<Attribute> embeddedAttributes = embeddableId.getAttributes();
                    Object embeddedObject = ((AbstractAttribute) attribute).getBindableJavaType().newInstance();
                    for (Attribute embeddedAttrib : embeddedAttributes) {
                        String embeddedColumnName = ((AbstractAttribute) embeddedAttrib).getJPAColumnName();

                        fieldMetadata = schemaTable.getField(embeddedColumnName);
                        value = row.get(embeddedColumnName);
                        NoSqlDBUtils.get(fieldMetadata, value, embeddedObject,
                                (Field) embeddedAttrib.getJavaMember());
                    }
                    PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), embeddedObject);

                } else {
                    fieldMetadata = schemaTable.getField(jpaColumnName);
                    value = row.get(jpaColumnName);

                    if (!attribute.isAssociation() && value != null) {
                        NoSqlDBUtils.get(fieldMetadata, value, entity, (Field) attribute.getJavaMember());
                    } else if (attribute.isAssociation() && value != null) {
                        Relation relation = entityMetadata.getRelation(attribute.getName());

                        if (relation != null) {
                            EntityMetadata associationMetadata = KunderaMetadataManager
                                    .getEntityMetadata(kunderaMetadata, relation.getTargetEntity());
                            if (!relation.getType().equals(ForeignKey.MANY_TO_MANY)) {
                                relationMap.put(jpaColumnName, NoSqlDBUtils.get(fieldMetadata, value,
                                        (Field) associationMetadata.getIdAttribute().getJavaMember()));
                            }
                        }
                    }
                }
            }
        }

        if (entity != null) {
            results.add(relationMap.isEmpty() ? entity
                    : new EnhanceEntity(entity,
                            key != null ? key : PropertyAccessorHelper.getId(entity, entityMetadata),
                            relationMap));
        }
    }
    return results;
}

From source file:com.impetus.client.cassandra.thrift.CQLTranslator.java

/**
 * On translation.//from  w w  w  .ja v  a  2 s  . c  om
 * 
 * @param record
 *            the record
 * @param m
 *            the m
 * @param type
 *            the type
 * @param metaModel
 *            the meta model
 * @param entityClazz
 *            the entity clazz
 * @param entityType
 *            the entity type
 * @param builders
 *            the builders
 * @param columnBuilders
 *            the column builders
 * @param externalProperties
 *            the external properties
 * @param kunderaMetadata
 *            the kundera metadata
 */
private void onTranslation(final Object record, final EntityMetadata m, TranslationType type,
        MetamodelImpl metaModel, Class entityClazz, EntityType entityType, Map<String, StringBuilder> builders,
        Map<String, StringBuilder> columnBuilders, Map<String, Object> externalProperties,
        final KunderaMetadata kunderaMetadata) {
    Set<Attribute> attributes = entityType.getAttributes();
    Iterator<Attribute> iterator = attributes.iterator();
    while (iterator.hasNext()) {
        Attribute attribute = iterator.next();

        // Populating table name.
        String tableName = ((AbstractAttribute) attribute).getTableName() != null
                ? ((AbstractAttribute) attribute).getTableName()
                : m.getTableName();

        StringBuilder columnBuilder = columnBuilders.get(tableName);
        if (columnBuilder == null) {
            columnBuilder = new StringBuilder();
            columnBuilders.put(tableName, columnBuilder);
        }

        StringBuilder builder = builders.get(tableName);
        if (builder == null) {
            builder = new StringBuilder();
            builders.put(tableName, builder);
        }
        Field field = (Field) attribute.getJavaMember();
        if (!attribute.equals(m.getIdAttribute()) && !((AbstractAttribute) attribute).getJPAColumnName()
                .equals(((AbstractAttribute) m.getIdAttribute()).getJPAColumnName())) {
            if (metaModel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
                // create embedded entity persisting format
                if (field.isAnnotationPresent(ElementCollection.class)) {
                    // handle embeddable collection
                    // check list, map, set
                    // build embedded value
                    StringBuilder elementCollectionValue = buildElementCollectionValue(field, record, metaModel,
                            attribute);
                    columnBuilder.append(Constants.ESCAPE_QUOTE);
                    columnBuilder.append(((AbstractAttribute) attribute).getJPAColumnName());
                    columnBuilder.append(Constants.ESCAPE_QUOTE);
                    columnBuilder.append(Constants.COMMA);
                    builder.append(elementCollectionValue);
                    builder.append(Constants.COMMA);
                } else {
                    EmbeddableType embeddableKey = metaModel.embeddable(field.getType());
                    Object embeddableKeyObj = PropertyAccessorHelper.getObject(record, field);
                    if (embeddableKeyObj != null) {

                        StringBuilder embeddedValueBuilder = new StringBuilder(Constants.OPEN_CURLY_BRACKET);

                        for (Field embeddableColumn : field.getType().getDeclaredFields()) {
                            if (!ReflectUtils.isTransientOrStatic(embeddableColumn)) {
                                AbstractAttribute subAttribute = (AbstractAttribute) embeddableKey
                                        .getAttribute(embeddableColumn.getName());
                                if (metaModel.isEmbeddable(subAttribute.getBindableJavaType())) {
                                    // construct map; recursive
                                    // send attribute
                                    if (embeddableColumn.isAnnotationPresent(ElementCollection.class)) {
                                        // build element collection value
                                        StringBuilder elementCollectionValue = buildElementCollectionValue(
                                                embeddableColumn, embeddableKeyObj, metaModel,
                                                (Attribute) subAttribute);

                                        appendColumnName(embeddedValueBuilder,
                                                ((AbstractAttribute) (embeddableKey
                                                        .getAttribute(embeddableColumn.getName())))
                                                                .getJPAColumnName());
                                        embeddedValueBuilder.append(Constants.COLON);
                                        embeddedValueBuilder.append(elementCollectionValue);
                                    } else {
                                        buildEmbeddedValue(embeddableKeyObj, metaModel, embeddedValueBuilder,
                                                (SingularAttribute) subAttribute);
                                    }
                                } else {
                                    // append key value
                                    appendColumnName(embeddedValueBuilder,
                                            ((AbstractAttribute) (embeddableKey
                                                    .getAttribute(embeddableColumn.getName())))
                                                            .getJPAColumnName());
                                    embeddedValueBuilder.append(Constants.COLON);
                                    appendColumnValue(embeddedValueBuilder, embeddableKeyObj, embeddableColumn);
                                }
                                embeddedValueBuilder.append(Constants.COMMA);
                            }
                        }
                        // strip last char and append '}'
                        embeddedValueBuilder.deleteCharAt(embeddedValueBuilder.length() - 1);
                        embeddedValueBuilder.append(Constants.CLOSE_CURLY_BRACKET);
                        // add to columnbuilder and builder
                        columnBuilder.append(Constants.ESCAPE_QUOTE);
                        columnBuilder.append(((AbstractAttribute) attribute).getJPAColumnName());
                        columnBuilder.append(Constants.ESCAPE_QUOTE);
                        columnBuilder.append(Constants.COMMA);
                        builder.append(embeddedValueBuilder);
                        builder.append(Constants.COMMA);
                        // end if
                    }
                }
            } else if (!ReflectUtils.isTransientOrStatic(field) && !attribute.isAssociation()) {
                onTranslation(type, builder, columnBuilder, ((AbstractAttribute) attribute).getJPAColumnName(),
                        record, field);
            }
        }
    }

    for (String tableName : columnBuilders.keySet()) {
        translateCompositeId(record, m, type, metaModel, builders, columnBuilders, externalProperties,
                kunderaMetadata, tableName, m.getIdAttribute());
    }

    // on inherited columns.
    onDiscriminatorColumn(builders.get(m.getTableName()), columnBuilders.get(m.getTableName()), entityType);
}