Example usage for javax.persistence.metamodel SingularAttribute getName

List of usage examples for javax.persistence.metamodel SingularAttribute getName

Introduction

In this page you can find the example usage for javax.persistence.metamodel SingularAttribute getName.

Prototype

String getName();

Source Link

Document

Return the name of the attribute.

Usage

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

public static String[] toNames(SingularAttribute<?, ?>... attributes) {
    List<String> ret = newArrayList();
    for (SingularAttribute<?, ?> attribute : attributes) {
        ret.add(attribute.getName());
    }/*from w w  w  . ja va  2  s.c  om*/
    return ret.toArray(new String[ret.size()]);
}

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

public static List<String> toNamesList(List<SingularAttribute<?, ?>> attributes) {
    List<String> ret = newArrayList();
    for (SingularAttribute<?, ?> attribute : attributes) {
        ret.add(attribute.getName());
    }//  ww  w. j av  a 2  s .c  o m
    return ret;
}

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

public static <T> boolean isPk(ManagedType<T> mt, SingularAttribute<? super T, ?> attr) {
    try {/*  w  ww .j  av a2s  .co  m*/
        Method m = BeanUtils.findMethod(mt.getJavaType(), "get" + WordUtils.capitalize(attr.getName()));
        if (m != null && m.getAnnotation(Id.class) != null) {
            return true;
        }

        Field field = mt.getJavaType().getField(attr.getName());
        return field.getAnnotation(Id.class) != null;
    } catch (Exception e) {
        return false;
    }
}

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

public static String getVersionFieldName(EntityManagerFactory emf, Class entityClass) {
    EntityType entityType = emf.getMetamodel().entity(entityClass);
    SingularAttribute version;
    if (entityType != null && (version = entityType.getVersion(Long.class)) != null) {
        return version.getName();
    }/*w ww  .ja v a2 s .  c o m*/
    return null;
}

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;
            }//from  w w  w.  j a v a  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:net.sf.companymanager.qbe.OrderBy.java

public OrderBy(final SingularAttribute<? extends Persistable, ? extends Serializable> attribute,
        final OrderByDirection direction) {
    Validate.notNull(attribute);//from  w w w  .  ja v a 2 s  .  co m
    Validate.notNull(direction);
    columnOrProperty = attribute.getName();
    this.direction = direction;
}

From source file:cz.datalite.dao.support.JpaMetamodelEntityInformation.java

public Iterable<String> getIdAttributeNames() {

    List<String> attributeNames = new ArrayList<String>(idMetadata.attributes.size());

    for (SingularAttribute<? super T, ?> attribute : idMetadata.attributes) {
        attributeNames.add(attribute.getName());
    }//  w w  w .  j a  v a  2s  .  co  m

    return attributeNames;
}

From source file:cz.datalite.dao.support.JpaMetamodelEntityInformation.java

@SuppressWarnings("unchecked")
public ID getId(T entity) {

    if (idMetadata.hasSimpleId()) {
        return (ID) getFieldValue(idMetadata.getSimpleIdAttribute().getName(), entity);
    }//  www .  j  a  v  a2  s . c  o m

    Object id = null;
    try {
        id = idMetadata.getType().newInstance();
    } catch (InstantiationException e) {
        throw new IllegalStateException("Unable to create metadata instance: " + idMetadata.getType(), e);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Unable to create metadata instance: " + idMetadata.getType(), e);
    }
    boolean partialIdValueFound = false;

    for (SingularAttribute<? super T, ?> attribute : idMetadata) {
        Object propertyValue = getFieldValue(attribute.getName(), entity);

        if (propertyValue != null) {
            partialIdValueFound = true;
        }

        setFieldValue(attribute.getName(), id, propertyValue);
    }

    return (ID) (partialIdValueFound ? id : null);
}

From source file:things.jpa.JpaConnector.java

private String getIdProperty(Class entityClass) {
    String idProperty = null;//from   www. j  a  v  a  2  s .  c om
    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:com.dbs.sdwt.jpa.ByExampleUtil.java

@SuppressWarnings("unchecked")
public <T extends Identifiable<?>, M2O extends Identifiable<?>> List<Predicate> byExampleOnXToOne(
        ManagedType<T> mt, Root<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) {
    List<Predicate> predicates = newArrayList();
    for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) {
        if (attr.getPersistentAttributeType() == MANY_TO_ONE
                || attr.getPersistentAttributeType() == ONE_TO_ONE) {
            M2O m2oValue = (M2O) jpaUtil.getValue(mtValue, mt.getAttribute(attr.getName()));
            Class<M2O> m2oType = (Class<M2O>) attr.getBindableJavaType();
            Path<M2O> m2oPath = (Path<M2O>) mtPath.get(attr);
            ManagedType<M2O> m2oMt = em.getMetamodel().entity(m2oType);
            if (m2oValue != null) {
                if (m2oValue.isIdSet()) { // we have an id, let's restrict only on this field
                    predicates.add(builder.equal(m2oPath.get("id"), m2oValue.getId()));
                } else {
                    predicates.addAll(byExample(m2oMt, m2oPath, m2oValue, sp, builder));
                }/*from   w  w  w. ja v  a 2 s  .c o  m*/
            }
        }
    }
    return predicates;
}