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.apache.click.extras.jpa.JpaForm.java

/**
 * Applies the <tt>ClassMetadata</tt> validation database meta data to the
 * form fields./*from w w w .ja v a2s.  c o m*/
 * <p/>
 * The field validation attributes include:
 * <ul>
 * <li>required - is a mandatory field and cannot be null</li>
 * </ul>
 */
protected void applyMetaData() {
    if (metaDataApplied) {
        return;
    }

    try {
        Class valueClass = Class.forName(classField.getValue());

        /*
        String classname = getClassname(valueClass);
                
        Metamodel metadata =
        ((HibernateEntityManagerFactory) getEntityManagerFactory()).getSessionFactory().getMetamodel(classname);
                
        String[] propertyNames = metadata.getPropertyNames();
                
        boolean[] propertyNullability = metadata.getPropertyNullability();
                 
        for (int i = 0; i < propertyNames.length; i++) {
        Field field = getField(propertyNames[i]);
        if (field != null) {
            field.setRequired(propertyNullability[i]);
        }
        }
         */
        Metamodel classMetadata = getEntityManager().getMetamodel();
        EntityType entityType = classMetadata.entity(valueClass);

        Set<SingularAttribute> attrs = entityType.getAttributes();

        for (SingularAttribute a : attrs) {
            Field field = getField(a.getName());
            if (field != null) {
                field.setRequired(a.isOptional());
            }
        }
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException(cnfe);
    }

    metaDataApplied = true;
}

From source file:org.apache.openejb.util.proxy.QueryProxy.java

private void remove(final Object[] args, final Class<?> returnType) {
    if (args != null && args.length == 1 && returnType.equals(Void.TYPE)) {
        Object entity = args[0];/*from   w w  w  .j a v  a2s  . c  o  m*/
        if (!em.contains(entity)) { // reattach the entity if possible
            final Class<?> entityClass = entity.getClass();
            final EntityType<? extends Object> et = em.getMetamodel().entity(entityClass);

            if (!et.hasSingleIdAttribute()) {
                throw new IllegalArgumentException("Dynamic EJB doesn't manage IdClass yet");
            }

            SingularAttribute<?, ?> id = null; // = et.getId(entityClass); doesn't work with openJPA
            for (final SingularAttribute<?, ?> sa : et.getSingularAttributes()) {
                if (sa.isId()) {
                    id = sa;
                    break;
                }
            }
            if (id == null) {
                throw new IllegalArgumentException("id field not found");
            }
            final String idName = id.getName();

            final Object idValue;
            try {
                idValue = BeanUtils.getProperty(entity, idName);
            } catch (final InvocationTargetException e) {
                throw new IllegalArgumentException("can't invoke to get entity id");
            } catch (final NoSuchMethodException e) {
                throw new IllegalArgumentException("can't find the method to get entity id");
            } catch (final IllegalAccessException e) {
                throw new IllegalArgumentException("can't access field/method to get entity id");
            }

            entity = em.getReference(et.getJavaType(), idValue);
            if (entity == null) {
                throw new IllegalArgumentException("entity " + entity + " is not managed and can't be found.");
            }
        }
        em.remove(entity);
    } else {
        throw new IllegalArgumentException(REMOVE_NAME + " should have only one parameter and return void");
    }
}

From source file:org.batoo.jpa.core.impl.criteria.join.AbstractFrom.java

/**
 * {@inheritDoc}/* www .ja v a 2  s. co m*/
 * 
 */
@Override
public <Y> AbstractJoin<X, Y> join(SingularAttribute<? super X, Y> attribute, JoinType jt) {
    return this.join(attribute.getName(), jt);
}

From source file:org.batoo.jpa.core.impl.criteria.join.FetchParentImpl.java

/**
 * {@inheritDoc}/* ww w. jav  a 2 s .  c o m*/
 * 
 */
@Override
public final <Y> FetchImpl<X, Y> fetch(SingularAttribute<? super X, Y> attribute, JoinType jt) {
    return this.fetch(attribute.getName(), jt);
}

From source file:org.batoo.jpa.core.impl.model.EntityTypeImpl.java

/**
 * Returns if the method is an id method.
 * /* w  ww  .j a  v  a 2 s  . com*/
 * @param method
 *            the method
 * @return if the method is an id method
 * 
 * @since 2.0.0
 */
public boolean isIdMethod(Method method) {
    if (this.idMethods.containsKey(method)) { // if known id method, let go
        return true;
    }

    final String methodName = method.getName();
    if (methodName.startsWith("get") && (methodName.length() > 3)) { // check if id method
        for (final SingularAttribute<? super X, ?> attribute : this.getSingularAttributes()) {
            final String getterName = "get" + StringUtils.capitalize(attribute.getName());
            if (attribute.isId() && getterName.equals(method.getName())) {
                this.idMethods.put(method, method);
                return true;
            }
        }
    }

    return false;
}

From source file:org.easy.criteria.CriteriaComposer.java

/**
 * Adds the attribute to having clause of the entity
 * // w ww .j av a 2s  . c o m
 * @param function
 *            - {@link AggregateFunction}
 * @param attribute
 *            - SingularAttribute of the entity for which this search was
 *            created.
 * @param nOperator
 *            - {@link NegationOperator}
 * @param cOperator
 *            - {@link ComparisonOperator}
 * @param value
 *            - value to match with the attribute.
 * @param lOperator
 *            - {@link LogicOperator} logic operator that will be applied
 *            with next having.
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public <V> CriteriaComposer<E> having(AggregateFunction function, SingularAttribute<? super E, V> attribute,
        NegationOperator nOperator, ComparisonOperator cOperator, V value) {

    Preconditions.checkArgument(_selects.indexOf(new SelectContainer(attribute, "")) >= 0,
            "Unable to find related select clasue.");

    Preconditions.checkNotNull(function);
    Preconditions.checkNotNull(attribute);
    Preconditions.checkNotNull(nOperator);
    Preconditions.checkNotNull(cOperator);

    HavingContainer<E> havingContainer = new HavingContainer<E>(function, attribute, nOperator, cOperator,
            value);

    int index = _havings.indexOf(havingContainer);

    if (index >= 0)
        _havings.add(index, havingContainer);
    else
        _havings.add(havingContainer);

    log.debug("Adding having for " + attribute.getName() + " " + cOperator.toString() + " value "
            + value.toString());

    lastCallType = LastCallType.HAVING;

    return this;
}

From source file:org.easy.criteria.CriteriaComposer.java

/**
 * Creates a singular join with the next entity.
 * //from w ww.  j  a  v a 2 s  .  c  om
 * @param <R>
 *            - The entity type to join with. (right side)
 * @param attribute
 *            - SingularAttribute of this entity that has reference to next
 *            entity
 * @return - Sub criteria for the next entity
 */
@SuppressWarnings("unchecked")
public <V> CriteriaComposer<V> join(JoinType joinType, SingularAttribute<? super E, V> attribute,
        CriteriaComposer<V> subCriteria) {
    Preconditions.checkNotNull(attribute);

    Class<V> classToJoin = attribute.getBindableJavaType();

    JoinContainer<E> join = new JoinContainer<E>(joinType, attribute);

    // Don't overwrite join
    if (_joins.containsKey(join))
        return (CriteriaComposer<V>) _joins.get(join);

    if (subCriteria == null)
        subCriteria = new CriteriaComposer<V>(classToJoin);

    _joins.put(join, subCriteria);

    log.debug("Addeding join " + joinType.toString() + " on " + classToJoin.getSimpleName() + " "
            + attribute.getName());

    return subCriteria;
}

From source file:org.easy.criteria.CriteriaComposer.java

/**
 * @param attribute//w w  w  .ja va  2 s.c o m
 * @param ascending
 * @param rank
 *            - precedence order - should be greater then zero and should be
 *            unique across the entire graph. Subsequent occurrences of the
 *            attribute with the same ranked number will be ignored and will
 *            not be added into orderBy clause.
 * 
 */
public <V> CriteriaComposer<E> orderBy(SingularAttribute<? super E, V> attribute, boolean ascending, int rank) {

    Preconditions.checkArgument(rank > 0);
    Preconditions.checkNotNull(attribute);

    log.debug("Adding _orderBy for " + attribute.getName());

    OrderByContainer<E> orderByContainer = new OrderByContainer<E>(attribute, ascending, rank);

    int index = _orderBys.indexOf(orderByContainer);

    if (index > -0)
        _orderBys.add(index, orderByContainer);
    else
        _orderBys.add(orderByContainer);

    return this;
}

From source file:org.easy.criteria.CriteriaComposer.java

/**
 * @param function/*w w w. j  av a 2s.  com*/
 * @param attribute
 * @param alias
 * @return
 */
public <V> CriteriaComposer<E> select(AggregateFunction function, SingularAttribute<? super E, V> attribute,
        String alias) {
    Preconditions.checkNotNull(function);
    Preconditions.checkNotNull(attribute);

    if (alias == null || alias.trim().length() == 0)
        alias = function.toString().toLowerCase() + "." + this._entityClass.getSimpleName() + "."
                + attribute.getName();

    SelectContainer<E> selectContainer = new SelectContainer<E>(function, attribute, alias);

    int index = _selects.indexOf(selectContainer);

    if (index >= 0)
        _selects.add(index, selectContainer);
    else
        _selects.add(selectContainer);

    log.debug("Added select " + attribute.toString());

    return this;
}

From source file:org.easy.criteria.CriteriaComposer.java

/**
 * @param attribute/*from  w  ww  .ja v a 2 s  .  com*/
 * @param alias
 */
public <V> CriteriaComposer<E> select(SingularAttribute<? super E, V> attribute, String alias) {
    Preconditions.checkNotNull(attribute);

    if (alias == null || alias.trim().length() == 0)
        alias = this._entityClass.getSimpleName() + "." + attribute.getName();

    SelectContainer<E> selectContainer = new SelectContainer<E>(attribute, alias);

    // overwrite if exists.
    int index = _selects.indexOf(selectContainer);

    if (index >= 0)
        _selects.add(index, selectContainer);
    else
        _selects.add(selectContainer);

    log.debug("Added select for " + attribute.getName() + "with alias " + alias);

    return this;
}