Example usage for javax.persistence PersistenceUnitUtil isLoaded

List of usage examples for javax.persistence PersistenceUnitUtil isLoaded

Introduction

In this page you can find the example usage for javax.persistence PersistenceUnitUtil isLoaded.

Prototype

public boolean isLoaded(Object entity, String attributeName);

Source Link

Document

Determine the load state of a given persistent attribute of an entity belonging to the persistence unit.

Usage

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

/**
 * Initialize a entity. /* w ww  .j a v a 2s. c om*/
 * @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);
                }
            }
        }
    }
}