@Override
 public EOQualifier qualifierMigratedFromEntityRelationshipPath(
     EOQualifier eoqualifier, EOEntity eoentity, String relationshipPath) {
   ERXToManyQualifier qualifier = (ERXToManyQualifier) eoqualifier;
   String newPath =
       EOQualifierSQLGeneration.Support._translateKeyAcrossRelationshipPath(
           qualifier.key(), relationshipPath, eoentity);
   return new ERXToManyQualifier(newPath, qualifier.elements(), qualifier.minCount());
 }
    @Override
    @SuppressWarnings("unchecked")
    public String sqlStringForSQLExpression(EOQualifier eoqualifier, EOSQLExpression e) {
      ERXToManyQualifier qualifier = (ERXToManyQualifier) eoqualifier;
      StringBuilder result = new StringBuilder();
      EOEntity targetEntity = e.entity();

      NSArray<String> toManyKeys = NSArray.componentsSeparatedByString(qualifier.key(), ".");
      EORelationship targetRelationship = null;
      for (int i = 0; i < toManyKeys.count() - 1; i++) {
        targetRelationship = targetEntity.anyRelationshipNamed(toManyKeys.objectAtIndex(i));
        targetEntity = targetRelationship.destinationEntity();
      }
      targetRelationship = targetEntity.relationshipNamed(toManyKeys.lastObject());
      targetEntity = targetRelationship.destinationEntity();

      if (targetRelationship.joins() == null || targetRelationship.joins().isEmpty()) {
        // we have a flattened many to many
        String definitionKeyPath = targetRelationship.definition();
        NSArray<String> definitionKeys =
            NSArray.componentsSeparatedByString(definitionKeyPath, ".");
        EOEntity lastStopEntity = targetRelationship.entity();
        EORelationship firstHopRelationship =
            lastStopEntity.relationshipNamed(definitionKeys.objectAtIndex(0));
        EOEntity endOfFirstHopEntity = firstHopRelationship.destinationEntity();
        EOJoin join = firstHopRelationship.joins().objectAtIndex(0); // assumes 1 join
        EOAttribute sourceAttribute = join.sourceAttribute();
        EOAttribute targetAttribute = join.destinationAttribute();
        EORelationship secondHopRelationship =
            endOfFirstHopEntity.relationshipNamed(definitionKeys.objectAtIndex(1));
        join = secondHopRelationship.joins().objectAtIndex(0); // assumes 1 join
        EOAttribute secondHopSourceAttribute = join.sourceAttribute();

        NSMutableArray<String> lastStopPKeyPath = toManyKeys.mutableClone();
        lastStopPKeyPath.removeLastObject();
        lastStopPKeyPath.addObject(firstHopRelationship.name());
        lastStopPKeyPath.addObject(targetAttribute.name());
        String firstHopRelationshipKeyPath = lastStopPKeyPath.componentsJoinedByString(".");
        result.append(e.sqlStringForAttributeNamed(firstHopRelationshipKeyPath));
        result.append(" IN ( SELECT ");

        result.append(lastStopEntity.externalName());
        result.append('.');
        result.append(lastStopEntity.primaryKeyAttributes().objectAtIndex(0).columnName());

        result.append(" FROM ");

        result.append(lastStopEntity.externalName());
        result.append(',');

        lastStopPKeyPath.removeLastObject();
        String tableAliasForJoinTable =
            (String)
                e.aliasesByRelationshipPath()
                    .objectForKey(
                        lastStopPKeyPath.componentsJoinedByString(".")); // "j"; //+random#
        result.append(endOfFirstHopEntity.externalName());
        result.append(' ');
        result.append(tableAliasForJoinTable);

        result.append(" WHERE ");

        appendColumnForAttributeToStringBuilder(sourceAttribute, result);
        result.append('=');
        result.append(e.sqlStringForAttributeNamed(firstHopRelationshipKeyPath));

        if (qualifier.elements() != null) {
          NSArray pKeys = ERXEOAccessUtilities.primaryKeysForObjects(qualifier.elements());
          result.append(" AND ");

          result.append(tableAliasForJoinTable);
          result.append('.');
          result.append(secondHopSourceAttribute.columnName());

          result.append(" IN (");
          EOAttribute pk = targetEntity.primaryKeyAttributes().lastObject();
          for (int i = 0; i < pKeys.count(); i++) {

            Object key = pKeys.objectAtIndex(i);
            String keyString = e.formatValueForAttribute(key, pk);
            // AK: default is is broken
            if ("NULL".equals(keyString)) {
              keyString = "" + key;
            }
            result.append(keyString);
            if (i < pKeys.count() - 1) {
              result.append(',');
            }
          }
          result.append(") ");
        }
        result.append(" GROUP BY ");
        appendColumnForAttributeToStringBuilder(sourceAttribute, result);

        result.append(" HAVING COUNT(*)");
        if (qualifier.minCount() <= 0) {
          result.append("=" + qualifier.elements().count());
        } else {
          result.append(">=" + qualifier.minCount());
        }
        result.append(" )");
      } else {
        throw new RuntimeException("not implemented!!");
      }
      return result.toString();
    }