/** Search the persistence context for an index of the child object, given a collection role */
  public Object getIndexInOwner(String entity, String property, Object childEntity, Map mergeMap) {

    EntityPersister persister = session.getFactory().getEntityPersister(entity);
    CollectionPersister cp = session.getFactory().getCollectionPersister(entity + '.' + property);
    Iterator entities = entityEntries.entrySet().iterator();
    while (entities.hasNext()) {
      Map.Entry me = (Map.Entry) entities.next();
      EntityEntry ee = (EntityEntry) me.getValue();
      if (persister.isSubclassEntityName(ee.getEntityName())) {
        Object instance = me.getKey();

        Object index = getIndexInParent(property, childEntity, persister, cp, instance);

        if (index == null && mergeMap != null) {
          Object unmergedInstance = mergeMap.get(instance);
          Object unmergedChild = mergeMap.get(childEntity);
          if (unmergedInstance != null && unmergedChild != null) {
            index = getIndexInParent(property, unmergedChild, persister, cp, unmergedInstance);
          }
        }

        if (index != null) return index;
      }
    }
    return null;
  }
  /**
   * Search the persistence context for an owner for the child object, given a collection role. If
   * <tt>mergeMap</tt> is non-null, also check the detached graph being merged for a parent.
   */
  public Serializable getOwnerId(String entity, String property, Object childEntity, Map mergeMap) {

    EntityPersister persister = session.getFactory().getEntityPersister(entity);
    final CollectionPersister collectionPersister =
        session.getFactory().getCollectionPersister(entity + '.' + property);

    Iterator entities = entityEntries.entrySet().iterator();
    while (entities.hasNext()) {
      Map.Entry me = (Map.Entry) entities.next();
      EntityEntry ee = (EntityEntry) me.getValue();
      if (persister.isSubclassEntityName(ee.getEntityName())) {
        Object instance = me.getKey();

        // check if the managed object is the parent
        boolean found =
            isFoundInParent(property, childEntity, persister, collectionPersister, instance);

        if (!found && mergeMap != null) {
          // check if the detached object being merged is the parent
          Object unmergedInstance = mergeMap.get(instance);
          Object unmergedChild = mergeMap.get(childEntity);
          if (unmergedInstance != null && unmergedChild != null) {
            found =
                isFoundInParent(
                    property, unmergedChild, persister, collectionPersister, unmergedInstance);
          }
        }

        if (found) {
          return ee.getId();
        }
      }
    }
    return null;
  }