Example usage for javax.persistence.metamodel EntityType getSingularAttributes

List of usage examples for javax.persistence.metamodel EntityType getSingularAttributes

Introduction

In this page you can find the example usage for javax.persistence.metamodel EntityType getSingularAttributes.

Prototype

Set<SingularAttribute<? super X, ?>> getSingularAttributes();

Source Link

Document

Return the single-valued attributes of the managed type.

Usage

From source file:nl.strohalm.cyclos.utils.database.DatabaseQueryHandler.java

/**
 * Copies the persistent properties from the source to the destination
 * entity/*from  w  ww.ja va 2s  .c  o  m*/
 */
public void copyProperties(final Entity source, final Entity dest) {
    if (source == null || dest == null) {
        return;
    }
    final EntityType metaData = getClassMetaData(source);

    final Set<Attribute> attrs = metaData.getSingularAttributes();

    for (Attribute attr : attrs) {

    }
    /*final Object[] values = metaData.getPropertyValues(source);
     // Skip the collections
     final Type[] types = metaData.getPropertyTypes();
     for (int i = 0; i < types.length; i++) {
     final Type type = types[i];
     if (type instanceof CollectionType) {
     values[i] = null;
     }
     }
     metaData.setPropertyValues(dest, values);*/
}

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  a 2s . 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");
    }
}