Example usage for javax.persistence.metamodel SingularAttribute isId

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

Introduction

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

Prototype

boolean isId();

Source Link

Document

Is the attribute an id attribute.

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;
            }//from ww  w .j  a  v  a2 s.  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  ww w  .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.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];// ww w  . j  av a 2 s  . 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.model.EntityTypeImpl.java

/**
 * Returns if the method is an id method.
 * /*from ww  w . j  a  v a2 s.c  om*/
 * @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;
}