Example usage for javax.persistence.metamodel EntityType getSingularAttributes

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

Introduction

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

Prototype

Set<SingularAttribute<? super X, ?>> getSingularAttributes();

Source Link

Document

Return the single-valued attributes of the managed type.

Usage

From source file:de.tudarmstadt.ukp.clarin.webanno.support.EntityModel.java

private void analyze(T aObject) {
    if (aObject != null) {
        entityClass = HibernateProxyHelper.getClassWithoutInitializingProxy(aObject);

        String idProperty = null;
        Metamodel metamodel = getEntityManager().getMetamodel();
        EntityType entity = metamodel.entity(entityClass);
        Set<SingularAttribute> singularAttributes = entity.getSingularAttributes();
        for (SingularAttribute singularAttribute : singularAttributes) {
            if (singularAttribute.isId()) {
                idProperty = singularAttribute.getName();
                break;
            }//  ww  w . j  a va  2s. c o m
        }
        if (idProperty == null) {
            throw new RuntimeException("id field not found");
        }

        DirectFieldAccessor accessor = new DirectFieldAccessor(aObject);
        id = (Number) accessor.getPropertyValue(idProperty);
    } else {
        entityClass = null;
        id = null;
    }
}

From source file:things.jpa.JpaConnector.java

private String getIdProperty(Class entityClass) {
    String idProperty = null;//from   w ww . jav a 2  s .co  m
    Metamodel metamodel = entityManager.getMetamodel();
    EntityType entity = metamodel.entity(entityClass);
    Set<SingularAttribute> singularAttributes = entity.getSingularAttributes();
    for (SingularAttribute singularAttribute : singularAttributes) {
        if (singularAttribute.isId()) {
            idProperty = singularAttribute.getName();
            break;
        }
    }
    if (idProperty == null)
        throw new RuntimeException("id field not found");
    return idProperty;
}

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

/**
 * _HACK_ too bad that JPA does not provide this entityType.getVersion();
 *
 * @see/*from w w  w.j  a  v a  2s. co  m*/
 * http://stackoverflow.com/questions/13265094/generic-way-to-get-jpa-entity-version
 */
private SingularAttribute<? super E, ?> getVersionAttribute(EntityType<E> entityType) {
    for (SingularAttribute<? super E, ?> sa : entityType.getSingularAttributes()) {
        if (sa.isVersion()) {
            return sa;
        }
    }
    return null;
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

private Predicate[] BuildFilterPredicates(Root<ENTITY> root, String search, List<String> searchProperties) {
    if (Strings.isNullOrEmpty(search)) {
        return new Predicate[] {};
    }/*from  www .  j  a  va 2  s .  com*/
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    EntityType<ENTITY> type = entityManager.getMetamodel().entity(entityClass);

    String[] split = search.split(",");

    Set<SingularAttribute<? super ENTITY, ?>> attributes = type.getSingularAttributes();
    List<Predicate> predicates = new ArrayList<Predicate>(split.length * attributes.size());
    for (String searchElem : split) {
        String searchProperty = null;
        if (searchElem.contains(":")) {
            String[] propSearchs = searchElem.trim().split(":", 2);
            searchElem = propSearchs[1];
            searchProperty = propSearchs[0];
        }

        boolean numeric;
        try {
            Double.parseDouble(searchElem);
            numeric = true;
        } catch (Exception e) {
            numeric = false;
        }
        for (SingularAttribute<? super ENTITY, ?> attribute : attributes) {
            if (searchProperties != null && !searchProperties.isEmpty()
                    && !searchProperties.contains(attribute.getName())) {
                continue; // skip this property as its not listed in searchable properties
            }
            if (searchProperty != null && !searchProperty.equals(attribute.getName())) {
                continue; // skip this property as we are searching for specific property
            }
            Class<?> javaType = attribute.getJavaType();
            if (javaType == String.class) {
                @SuppressWarnings("unchecked")
                Predicate like = builder.like(
                        builder.lower(root.get((SingularAttribute<ENTITY, String>) attribute)),
                        "%" + searchElem.toLowerCase().trim() + "%");
                predicates.add(like);
            } else if (numeric && (Number.class.isAssignableFrom(javaType) || javaType == int.class
                    || javaType == short.class || javaType == long.class || javaType == float.class
                    || javaType == double.class || javaType == byte.class)) {
                Predicate like = builder.equal(root.get(attribute), searchElem.toLowerCase().trim());
                predicates.add(like);
            }
            //TODO fancy types
            // enums
            // char   
            // boolean   
        }
    }
    return predicates.toArray(new Predicate[] {});
}

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

/**
 * Populates a {@link Relationship} with attributes of a given relationship
 * entity object <code>relationshipObj</code>
 * //from   w ww.  j av  a2  s. c om
 * @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

/**
 * Populates Node properties from Entity object
 * /*from   ww w  .  j  a va2 s  .c  om*/
 * @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//from   w  ww.jav a  2 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.neo4j.GraphEntityMapper.java

/**
 * /*from www . jav a  2  s  .  c o m*/
 * 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

/**
 * Creates a Map containing all properties (and their values) for a given
 * relationship entity/*from   w w  w  .  ja  va 2  s  .  c om*/
 * 
 * @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

/**
 * //from  w w w.  j a  v a2  s  . c o  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;
}