@Override
 public void appendTo(HqlQuery query, HqlQueryBuilderParams params) {
   HqlQueryValueImpl from = new HqlQueryValueImpl();
   from.appendHql(helper.getEntityName(root.getPropertyType()));
   from.appendHql(" ").append(root.getAlias());
   for (TypeSafeQueryJoin<?> join : joins) {
     TypeSafeQueryProxyData data = join.getData();
     if (data.getProxy() == null) {
       throw new IllegalStateException(
           format("Data [%s] was added as a join, but does not have a proxy.", data));
     }
     JoinType effectiveJoinType = data.getEffectiveJoinType();
     if (effectiveJoinType == null) {
       throw new IllegalArgumentException(
           "The getter for ["
               + data.getProxy()
               + "] was called, "
               + "but it was not passed to query.join(object, jointype).");
     }
     if (data.getPropertyPath() == null) {
       appendClassJoin(from, join, params);
     } else if (effectiveJoinType != JoinType.None) {
       if (isChildOfClassJoin(join)) {
         appendPropertyAsClassJoin(from, join, params);
       } else {
         appendPropertyJoin(from, join, params);
       }
     }
   }
   query.appendFrom(from.getHql());
   query.addParams(from.getParams());
 }
  /** Add class join while validating an on case was specified. */
  private void appendClassJoin(
      HqlQueryValueImpl from, TypeSafeQueryJoin<?> join, HqlQueryBuilderParams params) {
    // example: 'left join Product hobj1 on ...'
    TypeSafeQueryProxyData data = join.getData();
    from.appendHql(
        new StringBuilder(" ")
            .append(getJoinTypeString(data.getEffectiveJoinType()))
            .append(" ")
            .append(helper.getEntityName(data.getPropertyType()))
            .append(" ")
            .append(data.getAlias())
            .toString());

    // add the user specified on restrictions, there should at least but some restriction:
    if (!appendAdditionalRestrictions(from, join, " on ", params)) {
      if (params.isBuildingForDisplay()) {
        from.appendHql(" on ????? ");
      } else {
        throw new IllegalStateException(
            String.format(
                "Class join without on restriction exists for class [%s]", data.getPropertyType()));
      }
    }
  }
  /**
   * Create a join on for a property joined entity which is the child of a class joined entity. This
   * is a work around because hibernate doesn't properly convert the hql to sql in this case. (the
   * hql looks fine, but the sql throws an SqlGrammarException and the sql property join is missing)
   */
  private void appendPropertyAsClassJoin(
      HqlQueryValueImpl from, TypeSafeQueryJoin<?> join, HqlQueryBuilderParams params) {
    // example: 'left join Product hobj1 on ...'
    TypeSafeQueryProxyData data = join.getData();
    String alias = data.getAlias();
    StringBuilder joinSB =
        new StringBuilder(" ")
            .append(getJoinTypeString(data.getEffectiveJoinType()))
            .append(" ")
            .append(helper.getEntityName(data.getPropertyType()))
            .append(" ")
            .append(alias)
            .append(" on ");
    if (data.getProxyType().isCollection()) {
      String parentId = data.getParent().getIdentifierPath();
      // parent.parentIdentifier = child.parentPath.parentIdentifier
      String mappedByProperty = helper.getMappedByProperty(data);
      joinSB
          .append(data.getParent().getAlias())
          .append(".")
          .append(parentId)
          .append(" = ")
          .append(alias)
          .append(".")
          .append(mappedByProperty)
          .append(".")
          .append(parentId);
    } else {
      String childId = data.getIdentifierPath();
      // child.childIdentifier = parent.childPath.childIdentifier
      joinSB
          .append(alias)
          .append(".")
          .append(childId)
          .append(" = ")
          .append(data.getParent().getAlias())
          .append(".")
          .append(data.getPropertyPath())
          .append(".")
          .append(childId);
    }
    from.appendHql(joinSB.toString());

    // append additional "with" restrictions if such exist (continue after identity restriction):
    appendAdditionalRestrictions(from, join, " and ", params);
  }