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.eclecticlogic.pedal.dm.internal.MetamodelUtil.java

/**
 * @param attribute JPA metamodel attribute.
 * @param entity Entity to set the value on.
 * @param value Value to set.//w  ww  .  j  av  a 2 s  .c  o  m
 */
public static <E extends Serializable, T extends Serializable> void set(Attribute<? super E, T> attribute,
        E entity, T value) {
    Member member = attribute.getJavaMember();
    if (member instanceof Field) {
        Field field = (Field) member;
        field.setAccessible(true);
        try {
            field.set(entity, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else if (member instanceof Method) {
        PropertyDescriptor pd = BeanUtils.findPropertyForMethod((Method) member);
        if (pd.getWriteMethod() != null) {
            pd.getWriteMethod().setAccessible(true);
            try {
                pd.getWriteMethod().invoke(entity, value);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new RuntimeException(
                    "No setter for " + attribute.getName() + " in " + entity.getClass().getName());
        }
    } else {
        throw new RuntimeException("Failed to set " + attribute.getName() + " of type "
                + member.getClass().getName() + " in entity " + entity.getClass().getName());
    }
}

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

public static <T> Object getValue(T example, Attribute<? super T, ?> attr) {
    try {// www .  j a  v a  2 s . c  o  m
        if (attr.getJavaMember() instanceof Method) {
            return ReflectionUtils.invokeMethod((Method) attr.getJavaMember(), example);
        } else {
            return ReflectionUtils.getField((Field) attr.getJavaMember(), example);
        }
    } catch (Exception e) {
        throw propagate(e);
    }
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Gets the mappedBy value from an attribute
 * @param attribute attribute//from w w w .j a  v a 2s .  co m
 * @return mappedBy value or null if none.
 */
public static String getMappedBy(Attribute<?, ?> attribute) {
    String mappedBy = null;

    if (attribute.isAssociation()) {
        Annotation[] annotations = null;
        Member member = attribute.getJavaMember();
        if (member instanceof Field) {
            annotations = ((Field) member).getAnnotations();
        } else if (member instanceof Method) {
            annotations = ((Method) member).getAnnotations();
        }

        for (Annotation a : annotations) {
            if (a.annotationType().equals(OneToMany.class)) {
                mappedBy = ((OneToMany) a).mappedBy();
                break;
            } else if (a.annotationType().equals(ManyToMany.class)) {
                mappedBy = ((ManyToMany) a).mappedBy();
                break;
            } else if (a.annotationType().equals(OneToOne.class)) {
                mappedBy = ((OneToOne) a).mappedBy();
                break;
            }
        }
    }

    return "".equals(mappedBy) ? null : mappedBy;
}

From source file:com.impetus.kundera.utils.KunderaCoreUtils.java

/**
 * recursively populate all the fields present in partition key
 * //w  w w . j  ava  2s.  co m
 * @param embeddedAttributes
 * @param metaModel
 * @param embeddedIdFields
 */
private static void populateEmbeddedIdFields(Set<Attribute> embeddedAttributes, MetamodelImpl metaModel,
        Set<String> embeddedIdFields) {
    for (Attribute attribute : embeddedAttributes) {
        if (!ReflectUtils.isTransientOrStatic((Field) attribute.getJavaMember())) {
            if (metaModel.isEmbeddable(attribute.getJavaType())) {
                EmbeddableType embeddable = metaModel.embeddable(attribute.getJavaType());
                populateEmbeddedIdFieldsUtil(embeddable.getAttributes(), metaModel, embeddedIdFields);
            }
        }
    }
}

From source file:com.impetus.kundera.utils.KunderaCoreUtils.java

private static void populateEmbeddedIdFieldsUtil(Set<Attribute> embeddedAttributes, MetamodelImpl metaModel,
        Set<String> embeddedIdFields) {
    for (Attribute attribute : embeddedAttributes) {
        if (!ReflectUtils.isTransientOrStatic((Field) attribute.getJavaMember())) {
            if (metaModel.isEmbeddable(attribute.getJavaType())) {
                EmbeddableType embeddable = metaModel.embeddable(attribute.getJavaType());
                populateEmbeddedIdFieldsUtil(embeddable.getAttributes(), metaModel, embeddedIdFields);
            } else {
                String columnName = ((AbstractAttribute) attribute).getJPAColumnName();
                embeddedIdFields.add(columnName);
            }//from   w w w  .jav a  2 s .  c o  m
        }
    }
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

/**
 * Creates the columns field map./* w w  w.j  a  va  2  s  . c  om*/
 * 
 * @param m
 *            the m
 * @param superColumn
 *            the super column
 * @return the map
 */
public static Map<String, Field> createColumnsFieldMap(EntityMetadata m, EmbeddableType superColumn) {
    Map<String, Field> columnNameToFieldMap = new HashMap<String, Field>();

    Set<Attribute> attributes = superColumn.getAttributes();
    for (Attribute column : attributes) {
        columnNameToFieldMap.put(((AbstractAttribute) column).getJPAColumnName(),
                (Field) column.getJavaMember());
    }
    return columnNameToFieldMap;
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

/**
 * Checks whether an entity with given metadata contains a collection field
 * /* www.  ja  va  2  s  .c o  m*/
 * @param m
 * @return
 */
public static boolean containsBasicElementCollectionField(final EntityMetadata m,
        final KunderaMetadata kunderaMetadata) {
    Metamodel metaModel = kunderaMetadata.getApplicationMetadata().getMetamodel(m.getPersistenceUnit());
    EntityType entityType = metaModel.entity(m.getEntityClazz());
    Iterator<Attribute> iter = entityType.getAttributes().iterator();
    while (iter.hasNext()) {
        Attribute attr = iter.next();

        if (attr.isCollection() && !attr.isAssociation()
                && isBasicElementCollectionField((Field) attr.getJavaMember())) {
            return true;
        }
    }
    return false;
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

private static void getEmbeddableType(EntityMetadata m, Map<String, Field> columnNameToFieldMap,
        Map<String, Field> superColumnNameToFieldMap, final KunderaMetadata kunderaMetadata) {
    Metamodel metaModel = kunderaMetadata.getApplicationMetadata().getMetamodel(m.getPersistenceUnit());

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

    Set attributes = entityType.getAttributes();
    Iterator<Attribute> iter = attributes.iterator();
    while (iter.hasNext()) {
        Attribute attribute = iter.next();
        if (((MetamodelImpl) metaModel).isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
            superColumnNameToFieldMap.put(((AbstractAttribute) attribute).getJPAColumnName(),
                    (Field) attribute.getJavaMember());
            if (columnNameToFieldMap != null) {
                getAttributeOfEmbedddable(columnNameToFieldMap, metaModel, attribute);
            }/*from www  .  jav a2 s  .  com*/

        } else {
            if (columnNameToFieldMap != null) {
                columnNameToFieldMap.put(((AbstractAttribute) attribute).getJPAColumnName(),
                        (Field) attribute.getJavaMember());
            }
        }
    }
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

private static void getAttributeOfEmbedddable(Map<String, Field> columnNameToFieldMap, Metamodel metaModel,
        Attribute attribute) {/*w  ww  .j  av a  2  s . co m*/
    EmbeddableType embeddable = metaModel.embeddable(((AbstractAttribute) attribute).getBindableJavaType());

    Iterator<Attribute> embeddableIter = embeddable.getAttributes().iterator();
    while (embeddableIter.hasNext()) {
        Attribute embedAttrib = embeddableIter.next();

        // Reason is to avoid in case embeddable attribute within
        // embeddable.
        if (!((MetamodelImpl) metaModel).isEmbeddable(embedAttrib.getJavaType())) {
            columnNameToFieldMap.put(((AbstractAttribute) embedAttrib).getJPAColumnName(),
                    (Field) embedAttrib.getJavaMember());
        } else {
            getAttributeOfEmbedddable(columnNameToFieldMap, metaModel, embedAttrib);
        }
    }
}

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

/**
 * Gets the column meta data./*from   w ww . j a  v  a2s. c om*/
 *
 * @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;
}