Example usage for javax.persistence.metamodel Attribute getJavaMember

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

Introduction

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

Prototype

java.lang.reflect.Member getJavaMember();

Source Link

Document

Return the java.lang.reflect.Member for the represented attribute.

Usage

From source file:com.github.gekoh.yagen.util.MappingUtils.java

private static void appendNonCollectionReferences(Set<Attribute> attributes, Map<String, Field> fields,
        String context) {/*from w ww  .j a  v a  2s. c  o  m*/
    for (Attribute attribute : attributes) {
        String attributeName = context + attribute.getName();

        if (attribute instanceof SingularAttribute
                && ((SingularAttribute) attribute).getType() instanceof EmbeddableType) {
            appendNonCollectionReferences(
                    ((EmbeddableType) ((SingularAttribute) attribute).getType()).getAttributes(), fields,
                    attributeName + ".");
        } else if (!attribute.isCollection()
                && attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.BASIC
                && attribute.getDeclaringType() instanceof EntityType) {
            fields.put(attributeName, (Field) attribute.getJavaMember());
        }
    }
}

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

public <T> Object getValue(T example, Attribute<? super T, ?> attr) {
    try {//  ww  w. j a va2  s.  c  om
        if (attr.getJavaMember() instanceof Method) {
            return ((Method) attr.getJavaMember()).invoke(example);
        } else {
            return ((Field) attr.getJavaMember()).get(example);
        }
    } catch (Exception e) {
        throw propagate(e);
    }
}

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

/**
 * Creates a Map containing all properties (and their values) for a given
 * relationship entity/*from w ww. ja va 2 s  .  c  o m*/
 * 
 * @param entityMetadata
 * @param targetEntityMetadata
 * @param relationshipProperties
 * @param relationshipEntity
 */
public Map<String, Object> createRelationshipProperties(EntityMetadata entityMetadata,
        EntityMetadata targetEntityMetadata, Object relationshipEntity) {
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());
    EntityType entityType = metaModel.entity(relationshipEntity.getClass());
    Map<String, Object> relationshipProperties = new HashMap<String, Object>();

    Set<Attribute> attributes = entityType.getSingularAttributes();
    for (Attribute attribute : attributes) {
        Field f = (Field) attribute.getJavaMember();
        if (!f.getType().equals(entityMetadata.getEntityClazz())
                && !f.getType().equals(targetEntityMetadata.getEntityClazz())) {
            String relPropertyName = ((AbstractAttribute) attribute).getJPAColumnName();
            Object value = PropertyAccessorHelper.getObject(relationshipEntity, f);
            relationshipProperties.put(relPropertyName, toNeo4JProperty(value));
        }
    }
    return relationshipProperties;
}

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

/**
 * Populates Node properties from Entity object
 * /*from   w ww .ja  v a  2 s .  c o  m*/
 * @param entity
 * @param m
 * @param node
 */
private void populateNodeProperties(Object entity, EntityMetadata m, Node node) {
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(m.getPersistenceUnit());
    EntityType entityType = metaModel.entity(m.getEntityClazz());

    // Iterate over entity attributes
    Set<Attribute> attributes = entityType.getSingularAttributes();
    for (Attribute attribute : attributes) {
        Field field = (Field) attribute.getJavaMember();

        // Set Node level properties
        if (!attribute.isCollection() && !attribute.isAssociation()
                && !((SingularAttribute) attribute).isId()) {
            String columnName = ((AbstractAttribute) attribute).getJPAColumnName();
            Object value = PropertyAccessorHelper.getObject(entity, field);
            if (value != null) {
                node.setProperty(columnName, toNeo4JProperty(value));
            }
        }
    }
}

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

/**
 * Creates a Map containing all properties (and their values) for a given
 * entity// w ww  .  j a  va2 s.  c om
 * 
 * @param entity
 * @param m
 * @return
 */
public Map<String, Object> createNodeProperties(Object entity, EntityMetadata m) {
    Map<String, Object> props = new HashMap<String, Object>();

    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(m.getPersistenceUnit());

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

    // Iterate over entity attributes
    Set<Attribute> attributes = entityType.getSingularAttributes();
    for (Attribute attribute : attributes) {
        Field field = (Field) attribute.getJavaMember();

        // Set Node level properties
        if (!attribute.isCollection() && !attribute.isAssociation()) {

            String columnName = ((AbstractAttribute) attribute).getJPAColumnName();
            Object value = PropertyAccessorHelper.getObject(entity, field);
            if (value != null) {
                props.put(columnName, toNeo4JProperty(value));
            }
        }
    }
    return props;
}

From source file:com.impetus.client.rdbms.query.RDBMSEntityReader.java

/**
 * Gets the sql query from jpa./*from ww  w. j a v  a2  s .  c  o  m*/
 * 
 * @param entityMetadata
 *            the entity metadata
 * @param relations
 *            the relations
 * @param primaryKeys
 *            the primary keys
 * @return the sql query from jpa
 */
public String getSqlQueryFromJPA(EntityMetadata entityMetadata, List<String> relations,
        Set<String> primaryKeys) {
    ApplicationMetadata appMetadata = kunderaMetadata.getApplicationMetadata();
    Metamodel metaModel = appMetadata.getMetamodel(entityMetadata.getPersistenceUnit());

    if (jpaQuery != null) {
        String query = appMetadata.getQuery(jpaQuery);
        boolean isNative = kunderaQuery != null ? kunderaQuery.isNative() : false;

        if (isNative) {
            return query != null ? query : jpaQuery;
        }
    }

    // Suffixing the UNDERSCORE instead of prefix as Oracle 11g complains
    // about invalid characters error while executing the request.
    String aliasName = entityMetadata.getTableName() + "_";

    StringBuilder queryBuilder = new StringBuilder("Select ");

    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());
    Set<Attribute> attributes = entityType.getAttributes();
    for (Attribute field : attributes) {
        if (!field.isAssociation() && !field.isCollection()
                && !((Field) field.getJavaMember()).isAnnotationPresent(ManyToMany.class)
                && !((Field) field.getJavaMember()).isAnnotationPresent(Transient.class)
                && !((MetamodelImpl) metaModel)
                        .isEmbeddable(((AbstractAttribute) field).getBindableJavaType())) {
            queryBuilder.append(aliasName);
            queryBuilder.append(".");
            queryBuilder.append(((AbstractAttribute) field).getJPAColumnName());
            queryBuilder.append(", ");
        }
    }

    // Handle embedded columns, add them to list.
    Map<String, EmbeddableType> embeddedColumns = ((MetamodelImpl) metaModel)
            .getEmbeddables(entityMetadata.getEntityClazz());
    for (EmbeddableType embeddedCol : embeddedColumns.values()) {
        Set<Attribute> embeddedAttributes = embeddedCol.getAttributes();
        for (Attribute column : embeddedAttributes) {
            queryBuilder.append(aliasName);
            queryBuilder.append(".");
            queryBuilder.append(((AbstractAttribute) column).getJPAColumnName());
            queryBuilder.append(", ");
        }
    }

    if (relations != null) {
        for (String relation : relations) {
            Relation rel = entityMetadata.getRelation(entityMetadata.getFieldName(relation));
            String r = MetadataUtils.getMappedName(entityMetadata, rel, kunderaMetadata);
            if (!((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName()
                    .equalsIgnoreCase(r != null ? r : relation) && rel != null
                    && !rel.getProperty().isAnnotationPresent(ManyToMany.class)
                    && !rel.getProperty().isAnnotationPresent(OneToMany.class)
                    && (rel.getProperty().isAnnotationPresent(OneToOne.class)
                            && StringUtils.isBlank(rel.getMappedBy())
                            || rel.getProperty().isAnnotationPresent(ManyToOne.class))) {
                queryBuilder.append(aliasName);
                queryBuilder.append(".");
                queryBuilder.append(r != null ? r : relation);
                queryBuilder.append(", ");
            }
        }
    }

    // Remove last ","
    queryBuilder.deleteCharAt(queryBuilder.lastIndexOf(","));

    queryBuilder.append(" From ");
    if (entityMetadata.getSchema() != null && !entityMetadata.getSchema().isEmpty()) {
        queryBuilder.append(entityMetadata.getSchema() + ".");
    }
    queryBuilder.append(entityMetadata.getTableName());
    queryBuilder.append(" ");
    queryBuilder.append(aliasName);
    // add conditions
    if (filter != null) {
        queryBuilder.append(" Where ");
    }

    // Append conditions
    onCondition(entityMetadata, (MetamodelImpl) metaModel, primaryKeys, aliasName, queryBuilder, entityType);

    return queryBuilder.toString();
}

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

/**
 * Populates a {@link Relationship} with attributes of a given relationship
 * entity object <code>relationshipObj</code>
 * /*  ww w. ja va  2s .c o m*/
 * @param entityMetadata
 * @param targetNodeMetadata
 * @param relationship
 * @param relationshipObj
 */
public void populateRelationshipProperties(EntityMetadata entityMetadata, EntityMetadata targetNodeMetadata,
        Relationship relationship, Object relationshipObj) {
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());
    EntityType entityType = metaModel.entity(relationshipObj.getClass());

    Set<Attribute> attributes = entityType.getSingularAttributes();
    for (Attribute attribute : attributes) {
        Field f = (Field) attribute.getJavaMember();
        if (!f.getType().equals(entityMetadata.getEntityClazz())
                && !f.getType().equals(targetNodeMetadata.getEntityClazz())) {
            EntityMetadata relMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
                    relationshipObj.getClass());
            String columnName = ((AbstractAttribute) attribute).getJPAColumnName();
            if (metaModel.isEmbeddable(relMetadata.getIdAttribute().getBindableJavaType())
                    && ((SingularAttribute) attribute).isId()) {
                // Populate Embedded attribute value into relationship
                Object id = PropertyAccessorHelper.getId(relationshipObj, relMetadata);
                Object serializedIdValue = serializeIdAttributeValue(relMetadata, metaModel, id);
                relationship.setProperty(columnName, serializedIdValue);

                // Populate attributes of embedded fields into relationship
                Set<Attribute> embeddableAttributes = metaModel
                        .embeddable(relMetadata.getIdAttribute().getBindableJavaType()).getSingularAttributes();
                for (Attribute embeddableAttribute : embeddableAttributes) {
                    String embeddedColumn = ((AbstractAttribute) embeddableAttribute).getJPAColumnName();
                    if (embeddedColumn == null)
                        embeddedColumn = embeddableAttribute.getName();
                    Object value = PropertyAccessorHelper.getObject(id,
                            (Field) embeddableAttribute.getJavaMember());

                    if (value != null)
                        relationship.setProperty(embeddedColumn, value);
                }
            } else {
                Object value = PropertyAccessorHelper.getObject(relationshipObj, f);
                relationship.setProperty(columnName, toNeo4JProperty(value));
            }

        }
    }

}

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

/**
 * //w w  w. ja v  a  2 s.  c om
 * Converts a {@link Node} instance to entity object
 */
public Object getEntityFromNode(Node node, EntityMetadata m) {
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(m.getPersistenceUnit());
    EntityType entityType = metaModel.entity(m.getEntityClazz());

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

    Object entity = null;

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

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

            // Set Entity level properties
            if (metaModel.isEmbeddable(m.getIdAttribute().getBindableJavaType())
                    && m.getIdAttribute().getJavaType().equals(field.getType())) {
                Object idValue = deserializeIdAttributeValue(m, (String) node.getProperty(columnName));
                if (idValue != null) {
                    entity = initialize(m, entity);
                    PropertyAccessorHelper.set(entity, field, idValue);
                }
            } else if (!attribute.isCollection() && !attribute.isAssociation()
                    && !((AbstractAttribute) m.getIdAttribute()).getJPAColumnName().equals(columnName)) {
                Object columnValue = node.getProperty(columnName, null);
                if (columnValue != null) {
                    entity = initialize(m, entity);
                    PropertyAccessorHelper.set(entity, field, fromNeo4JObject(columnValue, field));
                }
            }
        }

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

    return entity;
}

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

/**
 * //from w ww  .ja va 2s  . co  m
 * 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.cassandra.query.ResultIterator.java

/**
 * Id value in byte arr./*from   ww  w . j a v a2 s .  com*/
 * 
 * @return the byte[]
 */
private byte[] idValueInByteArr() {
    Object entity = results.get(results.size() - 1);
    Object id = PropertyAccessorHelper.getId(entity, entityMetadata);
    String idName = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName();
    Class idClazz = ((AbstractAttribute) entityMetadata.getIdAttribute()).getBindableJavaType();
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());

    EmbeddableType keyObj = null;
    ByteBuffer bytes = null;

    // if the key attribute is composite
    if (metaModel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) {
        keyObj = metaModel.embeddable(entityMetadata.getIdAttribute().getBindableJavaType());
        Field embeddedField = getPartitionKeyField();
        Attribute partitionKey = keyObj.getAttribute(embeddedField.getName());
        Object partitionKeyValue = PropertyAccessorHelper.getObject(id, (Field) partitionKey.getJavaMember());
        bytes = CassandraUtilities.toBytes(partitionKeyValue, (Field) partitionKey.getJavaMember());
    } else {
        bytes = query.getBytesValue(idName, entityMetadata, id);
    }
    return bytes.array();
}