Example usage for javax.persistence.metamodel EntityType getDeclaredAttributes

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

Introduction

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

Prototype

Set<Attribute<X, ?>> getDeclaredAttributes();

Source Link

Document

Return the attributes declared by the managed type.

Usage

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);// w ww . ja  va  2  s .  c o m
        }
    }
    return attributes;
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Initialize a entity. //from w w w .  ja v  a2s  .  co m
 * @param em entity manager to use
 * @param entity entity to initialize
 * @param depth max depth on recursion
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void initialize(EntityManager em, Object entity, int depth) {
    // return on nulls, depth = 0 or already initialized objects
    if (entity == null || depth == 0) {
        return;
    }

    PersistenceUnitUtil unitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();
    EntityType entityType = em.getMetamodel().entity(entity.getClass());
    Set<Attribute> attributes = entityType.getDeclaredAttributes();

    Object id = unitUtil.getIdentifier(entity);

    if (id != null) {
        Object attached = em.find(entity.getClass(), unitUtil.getIdentifier(entity));

        for (Attribute a : attributes) {
            if (!unitUtil.isLoaded(entity, a.getName())) {
                if (a.isCollection()) {
                    intializeCollection(em, entity, attached, a, depth);
                } else if (a.isAssociation()) {
                    intialize(em, entity, attached, a, depth);
                }
            }
        }
    }
}