Example usage for javax.persistence PersistenceUnitUtil getIdentifier

List of usage examples for javax.persistence PersistenceUnitUtil getIdentifier

Introduction

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

Prototype

public Object getIdentifier(Object entity);

Source Link

Document

Return the id of the entity.

Usage

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

/**
 * Initialize a entity. // www . j a va 2 s .c  o  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);
                }
            }
        }
    }
}