Ejemplo n.º 1
0
  /**
   * Attach the passed in entity with the EntityManager retrieved from Seam.
   *
   * @param entity
   * @return The attached entity
   */
  @Override
  public Object fetchEntity(Object entity, String[] fetch) {
    EntityManager em = (EntityManager) Component.getInstance(emName);
    if (em == null)
      throw new RuntimeException("Could not find entity, EntityManager [" + emName + "] not found");

    Serializable id = (Serializable) Entity.forClass(entity.getClass()).getIdentifier(entity);
    if (id == null) return null;

    if (fetch == null || em.getDelegate().getClass().getName().indexOf(".hibernate.") < 0)
      return em.find(entity.getClass(), id);

    for (String f : fetch) {
      Query q =
          em.createQuery(
              "select e from "
                  + entity.getClass().getName()
                  + " e left join fetch e."
                  + f
                  + " where e = :entity");
      q.setParameter("entity", entity);
      entity = q.getSingleResult();
    }
    return entity;
  }
  /**
   * Attach the passed in entity with the EntityManager stored in the EntityHome object.
   *
   * @param entity
   * @return the attached hibernate object
   */
  @Override
  public Object fetchEntity(Object entity, String[] fetch) {
    PersistenceController<?> controller =
        (PersistenceController<?>) Component.getInstance(controllerName);
    EntityManager em = (EntityManager) controller.getPersistenceContext();
    Serializable id = (Serializable) Entity.forClass(entity.getClass()).getIdentifier(entity);
    if (id == null) return null;

    if (fetch == null || em.getDelegate().getClass().getName().indexOf(".hibernate.") < 0)
      return em.find(entity.getClass(), id);

    for (String f : fetch) {
      Query q =
          em.createQuery(
              "select e from "
                  + entity.getClass().getName()
                  + " e left join fetch e."
                  + f
                  + " where e = :entity");
      q.setParameter("entity", entity);
      entity = q.getSingleResult();
    }
    return entity;
  }