Example usage for javax.persistence.metamodel Attribute getJavaType

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

Introduction

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

Prototype

Class<Y> getJavaType();

Source Link

Document

Return the Java type of the represented attribute.

Usage

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

public static <T, A> SingularAttribute<? super T, A> attribute(ManagedType<? super T> mt,
        Attribute<? super T, A> attr) {
    return mt.getSingularAttribute(attr.getName(), attr.getJavaType());
}

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

public static Set<Attribute> getCollectionDescriptors(EntityManagerFactory emf, Class entityClass) {
    Set<Attribute> attributes = new HashSet<Attribute>();
    EntityType entityType = emf.getMetamodel().entity(entityClass);
    for (Object o : entityType.getDeclaredAttributes()) {
        Attribute attribute = (Attribute) o;
        if (Collection.class.isAssignableFrom(attribute.getJavaType())) {
            attributes.add(attribute);/*from  ww  w .ja  v a  2 s. c  om*/
        }
    }
    return attributes;
}

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

/**
 * Gets the column meta data./*w  w w .  jav  a2  s  . 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.jdal.dao.jpa.JpaUtils.java

/**
 * Test if attribute is type or in collections has element type
 * @param attribute attribute to test//from  ww w . ja  v  a2 s. c o  m
 * @param clazz Class to test
 * @return true if clazz is asignable from type or element type
 */
public static boolean isTypeOrElementType(Attribute<?, ?> attribute, Class<?> clazz) {
    if (attribute.isCollection()) {
        return clazz.isAssignableFrom(((CollectionAttribute<?, ?>) attribute).getBindableJavaType());
    }

    return clazz.isAssignableFrom(attribute.getJavaType());
}

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

/**
 * recursively populate all the fields present in partition key
 * /*  w w  w.j a v  a  2 s  .com*/
 * @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. j av  a  2s.co  m*/
        }
    }
}

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

private static void getAttributeOfEmbedddable(Map<String, Field> columnNameToFieldMap, Metamodel metaModel,
        Attribute attribute) {/*from   w w  w  . j  av a2 s.  c  o  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:com.github.gekoh.yagen.util.MappingUtils.java

private static void fillTreeEntityMap(Map<Class, TreeEntity> treeEntities, Set<Attribute> attributes,
        Class entityClass) {// w  w  w. j ava2  s .com
    for (Attribute attribute : attributes) {
        if (attribute instanceof SingularAttribute
                && ((SingularAttribute) attribute).getType() instanceof EmbeddableType) {
            fillTreeEntityMap(treeEntities,
                    ((EmbeddableType) ((SingularAttribute) attribute).getType()).getAttributes(), entityClass);
        } else if (!attribute.isCollection()
                && attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.BASIC
                && attribute.getDeclaringType() instanceof EntityType) {
            Class targetEntity = attribute.getJavaType();
            addMasterEntity(treeEntities, entityClass, targetEntity);
        } else if (attribute.isCollection()
                && attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.MANY_TO_MANY
                && attribute instanceof PluralAttribute) {
            addMasterEntity(treeEntities, ((PluralAttribute) attribute).getElementType().getJavaType(),
                    entityClass);
        }
    }
    if (!entityClass.isAnnotationPresent(Table.class)) {
        Class lastEntity = entityClass;
        Class parent = lastEntity;
        do {
            parent = parent.getSuperclass();
            if (parent.isAnnotationPresent(Entity.class)) {
                addMasterEntity(treeEntities, parent, lastEntity);
                lastEntity = parent;
            }
            if (parent.isAnnotationPresent(Table.class)) {
                break;
            }
        } while (parent.getSuperclass() != null);
    }
}

From source file:bq.jpa.demo.metadata.service.MetadataService.java

public void doMD() {
    Metamodel md = em.getMetamodel();//from w  ww  .j a va  2s .co m
    EntityType<Employee> employee = md.entity(Employee.class);
    for (Attribute<? super Employee, ?> attr : employee.getAttributes()) {
        System.out.println(attr.getName() + " | " + attr.getJavaType().getName() + " | "
                + attr.getPersistentAttributeType());
    }
}

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

public <T, A> SingularAttribute<? super T, A> attribute(ManagedType<? super T> mt,
        Attribute<? super T, A> attr) {
    return mt.getSingularAttribute(attr.getName(), attr.getJavaType());
}