Program: 176301
Project: 176301
Buggy source:
/** 
 * Gets the sql query from jpa.
 * @param entityMetadata the entity metadata
 * @param relations the relations
 * @param primaryKeys the primary keys
 * @return the sql query from jpa
 */
public String getSqlQueryFromJPA(EntityMetadata entityMetadata,List<String> relations,Set<String> primaryKeys){
  ApplicationMetadata appMetadata=KunderaMetadata.INSTANCE.getApplicationMetadata();
  Metamodel metaModel=appMetadata.getMetamodel(entityMetadata.getPersistenceUnit());
  if (appMetadata.isNative(jpaQuery)) {
    return appMetadata.getQuery(jpaQuery);
  }
  String aliasName="String_Node_Str" + entityMetadata.getTableName();
  StringBuilder queryBuilder=new StringBuilder("String_Node_Str");
  queryBuilder.append(aliasName);
  queryBuilder.append("String_Node_Str");
  queryBuilder.append(((AbstractAttribute)entityMetadata.getIdAttribute()).getJPAColumnName());
  EntityType entityType=metaModel.entity(entityMetadata.getEntityClazz());
  Set<Attribute> attributes=entityType.getAttributes();
  for (  Attribute field : attributes) {
    if (!field.isAssociation() && !field.isCollection() && !((Field)field.getJavaMember()).isAnnotationPresent(ManyToMany.class)&& !((AbstractAttribute)field).getJPAColumnName().equals(((AbstractAttribute)entityMetadata.getIdAttribute()).getJPAColumnName())&& !((MetamodelImpl)metaModel).isEmbeddable(((AbstractAttribute)field).getBindableJavaType())) {
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(aliasName);
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(((AbstractAttribute)field).getJPAColumnName());
    }
  }
  Map<String,EmbeddableType> embeddedColumns=((MetamodelImpl)metaModel).getEmbeddables(entityMetadata.getEntityClazz());
  for (  EmbeddableType embeddedCol : embeddedColumns.values()) {
    Set<Attribute> embeddedAttributes=embeddedCol.getAttributes();
    for (    Attribute column : embeddedAttributes) {
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(aliasName);
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(((AbstractAttribute)column).getJPAColumnName());
    }
  }
  if (relations != null) {
    for (    String relation : relations) {
      Relation rel=entityMetadata.getRelation(entityMetadata.getFieldName(relation));
      String r=MetadataUtils.getMappedName(entityMetadata,rel);
      if (!((AbstractAttribute)entityMetadata.getIdAttribute()).getJPAColumnName().equalsIgnoreCase(r != null ? r : relation) && rel != null && !rel.getProperty().isAnnotationPresent(ManyToMany.class) && !rel.getProperty().isAnnotationPresent(OneToMany.class) && (rel.getProperty().isAnnotationPresent(OneToOne.class) && StringUtils.isBlank(rel.getMappedBy()) || rel.getProperty().isAnnotationPresent(ManyToOne.class))) {
        queryBuilder.append("String_Node_Str");
        queryBuilder.append(aliasName);
        queryBuilder.append("String_Node_Str");
        queryBuilder.append(r != null ? r : relation);
      }
    }
  }
  for (  Relation r : entityMetadata.getRelations()) {
    if (!r.getType().equals(ForeignKey.MANY_TO_MANY) && (r.getProperty().isAnnotationPresent(OneToOne.class) && StringUtils.isBlank(r.getMappedBy()) || r.getProperty().isAnnotationPresent(ManyToOne.class))) {
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(aliasName);
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(r.getJoinColumnName());
    }
  }
  queryBuilder.append("String_Node_Str");
  if (entityMetadata.getSchema() != null && !entityMetadata.getSchema().isEmpty()) {
    queryBuilder.append(entityMetadata.getSchema() + "String_Node_Str");
  }
  queryBuilder.append(entityMetadata.getTableName());
  queryBuilder.append("String_Node_Str");
  queryBuilder.append(aliasName);
  if (filter != null) {
    queryBuilder.append("String_Node_Str");
  }
  if (primaryKeys == null) {
    for (    Object o : conditions) {
      if (o instanceof FilterClause) {
        FilterClause clause=((FilterClause)o);
        String fieldName=clause.getProperty();
        boolean isString=isStringProperty(entityType,fieldName,entityMetadata);
        queryBuilder.append(StringUtils.replace(clause.getProperty(),aliasName,aliasName));
        queryBuilder.append("String_Node_Str");
        queryBuilder.append(clause.getCondition());
        if (clause.getCondition().equalsIgnoreCase("String_Node_Str")) {
          queryBuilder.append("String_Node_Str");
        }
        queryBuilder.append("String_Node_Str");
        appendStringPrefix(queryBuilder,isString);
        queryBuilder.append(clause.getValue());
        appendStringPrefix(queryBuilder,isString);
      }
 else {
        queryBuilder.append("String_Node_Str");
        queryBuilder.append(o);
        queryBuilder.append("String_Node_Str");
      }
    }
  }
 else {
    queryBuilder.append(aliasName);
    queryBuilder.append("String_Node_Str");
    queryBuilder.append(((AbstractAttribute)entityMetadata.getIdAttribute()).getJPAColumnName());
    queryBuilder.append("String_Node_Str");
    queryBuilder.append("String_Node_Str");
    int count=0;
    Attribute col=entityMetadata.getIdAttribute();
    boolean isString=col.getJavaType().isAssignableFrom(String.class);
    for (    String key : primaryKeys) {
      appendStringPrefix(queryBuilder,isString);
      queryBuilder.append(key);
      appendStringPrefix(queryBuilder,isString);
      if (++count != primaryKeys.size()) {
        queryBuilder.append("String_Node_Str");
      }
 else {
        queryBuilder.append("String_Node_Str");
      }
    }
  }
  return queryBuilder.toString();
}

Fixed version (ground truth):
/** 
 * Gets the sql query from jpa.
 * @param entityMetadata the entity metadata
 * @param relations the relations
 * @param primaryKeys the primary keys
 * @return the sql query from jpa
 */
public String getSqlQueryFromJPA(EntityMetadata entityMetadata,List<String> relations,Set<String> primaryKeys){
  ApplicationMetadata appMetadata=KunderaMetadata.INSTANCE.getApplicationMetadata();
  Metamodel metaModel=appMetadata.getMetamodel(entityMetadata.getPersistenceUnit());
  if (appMetadata.isNative(jpaQuery)) {
    return appMetadata.getQuery(jpaQuery);
  }
  String aliasName="String_Node_Str" + entityMetadata.getTableName();
  StringBuilder queryBuilder=new StringBuilder("String_Node_Str");
  queryBuilder.append(aliasName);
  queryBuilder.append("String_Node_Str");
  queryBuilder.append(((AbstractAttribute)entityMetadata.getIdAttribute()).getJPAColumnName());
  EntityType entityType=metaModel.entity(entityMetadata.getEntityClazz());
  Set<Attribute> attributes=entityType.getAttributes();
  for (  Attribute field : attributes) {
    if (!field.isAssociation() && !field.isCollection() && !((Field)field.getJavaMember()).isAnnotationPresent(ManyToMany.class)&& !((AbstractAttribute)field).getJPAColumnName().equals(((AbstractAttribute)entityMetadata.getIdAttribute()).getJPAColumnName())&& !((MetamodelImpl)metaModel).isEmbeddable(((AbstractAttribute)field).getBindableJavaType())) {
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(aliasName);
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(((AbstractAttribute)field).getJPAColumnName());
    }
  }
  Map<String,EmbeddableType> embeddedColumns=((MetamodelImpl)metaModel).getEmbeddables(entityMetadata.getEntityClazz());
  for (  EmbeddableType embeddedCol : embeddedColumns.values()) {
    Set<Attribute> embeddedAttributes=embeddedCol.getAttributes();
    for (    Attribute column : embeddedAttributes) {
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(aliasName);
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(((AbstractAttribute)column).getJPAColumnName());
    }
  }
  if (relations != null) {
    for (    String relation : relations) {
      Relation rel=entityMetadata.getRelation(entityMetadata.getFieldName(relation));
      String r=MetadataUtils.getMappedName(entityMetadata,rel);
      if (!((AbstractAttribute)entityMetadata.getIdAttribute()).getJPAColumnName().equalsIgnoreCase(r != null ? r : relation) && rel != null && !rel.getProperty().isAnnotationPresent(ManyToMany.class) && !rel.getProperty().isAnnotationPresent(OneToMany.class) && (rel.getProperty().isAnnotationPresent(OneToOne.class) && StringUtils.isBlank(rel.getMappedBy()) || rel.getProperty().isAnnotationPresent(ManyToOne.class))) {
        queryBuilder.append("String_Node_Str");
        queryBuilder.append(aliasName);
        queryBuilder.append("String_Node_Str");
        queryBuilder.append(r != null ? r : relation);
      }
    }
  }
  for (  Relation r : entityMetadata.getRelations()) {
    if (!r.getType().equals(ForeignKey.MANY_TO_MANY) && (r.getProperty().isAnnotationPresent(OneToOne.class) && StringUtils.isBlank(r.getMappedBy()) || r.getProperty().isAnnotationPresent(ManyToOne.class))) {
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(aliasName);
      queryBuilder.append("String_Node_Str");
      queryBuilder.append(r.getJoinColumnName());
    }
  }
  queryBuilder.append("String_Node_Str");
  if (entityMetadata.getSchema() != null && !entityMetadata.getSchema().isEmpty()) {
    queryBuilder.append(entityMetadata.getSchema() + "String_Node_Str");
  }
  queryBuilder.append(entityMetadata.getTableName());
  queryBuilder.append("String_Node_Str");
  queryBuilder.append(aliasName);
  if (filter != null) {
    queryBuilder.append("String_Node_Str");
  }
  if (primaryKeys == null) {
    for (    Object o : conditions) {
      if (o instanceof FilterClause) {
        FilterClause clause=((FilterClause)o);
        String fieldName=clause.getProperty();
        boolean isString=isStringProperty(entityType,fieldName,entityMetadata);
        queryBuilder.append(StringUtils.replace(clause.getProperty(),aliasName,aliasName));
        queryBuilder.append("String_Node_Str");
        queryBuilder.append(clause.getCondition());
        if (clause.getCondition().equalsIgnoreCase("String_Node_Str")) {
          queryBuilder.append("String_Node_Str");
        }
        queryBuilder.append("String_Node_Str");
        if (clause.getCondition().equalsIgnoreCase("String_Node_Str")) {
          buildINClause(queryBuilder,clause,isString);
        }
 else {
          appendStringPrefix(queryBuilder,isString);
          queryBuilder.append(clause.getValue());
          appendStringPrefix(queryBuilder,isString);
        }
      }
 else {
        queryBuilder.append("String_Node_Str");
        queryBuilder.append(o);
        queryBuilder.append("String_Node_Str");
      }
    }
  }
 else {
    queryBuilder.append(aliasName);
    queryBuilder.append("String_Node_Str");
    queryBuilder.append(((AbstractAttribute)entityMetadata.getIdAttribute()).getJPAColumnName());
    queryBuilder.append("String_Node_Str");
    queryBuilder.append("String_Node_Str");
    int count=0;
    Attribute col=entityMetadata.getIdAttribute();
    boolean isString=col.getJavaType().isAssignableFrom(String.class);
    for (    String key : primaryKeys) {
      appendStringPrefix(queryBuilder,isString);
      queryBuilder.append(key);
      appendStringPrefix(queryBuilder,isString);
      if (++count != primaryKeys.size()) {
        queryBuilder.append("String_Node_Str");
      }
 else {
        queryBuilder.append("String_Node_Str");
      }
    }
  }
  return queryBuilder.toString();
}