Program: 59413
Project: 59413
Buggy source:
/** 
 * Execute a select query on entityClass using   {@code DatatablesCriterias}information for filter, sort and paginate result.
 * @param entityClass entity to use in search
 * @param entityManager {@code entityClass} {@link EntityManager}
 * @param datatablesCriterias datatables parameters for query
 * @param distinct use distinct query
 * @return
 */
public static <T>FindResult<T> findByCriteria(Class<T> entityClass,EntityManager entityManager,DatatablesCriterias datatablesCriterias,boolean distinct){
  CriteriaBuilder builder=entityManager.getCriteriaBuilder();
  CriteriaQuery<T> query=builder.createQuery(entityClass);
  CriteriaQuery<Long> countQuery=builder.createQuery(Long.class);
  EntityType<T> entity=entityManager.getMetamodel().entity(entityClass);
  boolean isPaged=datatablesCriterias.getDisplaySize() != null && datatablesCriterias.getDisplaySize() > 0;
  Root<T> from=query.from(entityClass);
  Predicate filterCondition=null;
  Predicate searchCondition=null;
  try {
    if (datatablesCriterias.hasOneFilteredColumn()) {
      List<Predicate> condition=new ArrayList<Predicate>();
      for (      ColumnDef column : datatablesCriterias.getColumnDefs()) {
        if (column.isFilterable() && StringUtils.isNotBlank(column.getSearch())) {
          Predicate fieldlike=getFindCondition(entity,from,column.getName(),column.getSearch(),builder,true);
          if (fieldlike != null) {
            condition.add(fieldlike);
          }
        }
      }
      if (!condition.isEmpty()) {
        filterCondition=builder.and(condition.toArray(new Predicate[]{}));
      }
    }
    String toSearch=datatablesCriterias.getSearch();
    if (StringUtils.isNotBlank(toSearch) && datatablesCriterias.hasOneFilterableColumn()) {
      List<Predicate> condition=new ArrayList<Predicate>();
      for (      ColumnDef column : datatablesCriterias.getColumnDefs()) {
        if (column.isFilterable()) {
          Predicate fieldlike=getFindCondition(entity,from,column.getName(),toSearch,builder,false);
          if (fieldlike != null) {
            condition.add(fieldlike);
          }
        }
      }
      if (!condition.isEmpty()) {
        searchCondition=builder.or(condition.toArray(new Predicate[]{}));
      }
    }
  }
 catch (  EmptyResultException e) {
    return new FindResult<T>(new ArrayList<T>(0),0,isPaged,(long)datatablesCriterias.getDisplayStart(),datatablesCriterias.getDisplaySize());
  }
  if (filterCondition != null && searchCondition != null) {
    query.where(builder.and(filterCondition,searchCondition));
    countQuery.where(builder.and(filterCondition,searchCondition));
  }
 else   if (filterCondition != null) {
    query.where(filterCondition);
    countQuery.where(filterCondition);
  }
 else   if (searchCondition != null) {
    query.where(searchCondition);
    countQuery.where(searchCondition);
  }
  if (datatablesCriterias.hasOneSortedColumn()) {
    List<Order> orderList=new ArrayList<Order>();
    for (    ColumnDef column : datatablesCriterias.getSortingColumnDefs()) {
      if (column.getSortDirection() == null) {
        continue;
      }
 else       if (column.getSortDirection() == SortDirection.ASC) {
        orderList.add(builder.asc(from.get(column.getName())));
      }
 else {
        orderList.add(builder.desc(from.get(column.getName())));
      }
    }
    if (!orderList.isEmpty()) {
      query.orderBy(orderList);
    }
  }
  CriteriaQuery<T> select=query.select(from);
  CriteriaQuery<Long> count=null;
  if (distinct) {
    count=countQuery.select(builder.countDistinct(countQuery.from(entityClass)));
  }
 else {
    count=countQuery.select(builder.count(countQuery.from(entityClass)));
  }
  select.distinct(distinct);
  TypedQuery<T> typedQuery=entityManager.createQuery(select);
  long startItem=0;
  int pageSize=0;
  if (isPaged) {
    pageSize=datatablesCriterias.getDisplaySize();
    typedQuery.setMaxResults(pageSize);
  }
  if (datatablesCriterias.getDisplayStart() >= 0) {
    startItem=datatablesCriterias.getDisplayStart();
    typedQuery.setFirstResult((int)startItem);
  }
  List<T> elements=typedQuery.getResultList();
  long totalResultCount=elements.size();
  if (isPaged) {
    totalResultCount=entityManager.createQuery(count).getSingleResult();
  }
  return new FindResult<T>(elements,totalResultCount,isPaged,startItem,pageSize);
}

Fixed version (ground truth):
/** 
 * Execute a select query on entityClass using   {@code DatatablesCriterias}information for filter, sort and paginate result.
 * @param entityClass entity to use in search
 * @param entityManager {@code entityClass} {@link EntityManager}
 * @param datatablesCriterias datatables parameters for query
 * @param distinct use distinct query
 * @return
 */
public static <T>FindResult<T> findByCriteria(Class<T> entityClass,EntityManager entityManager,DatatablesCriterias datatablesCriterias,boolean distinct){
  CriteriaBuilder builder=entityManager.getCriteriaBuilder();
  CriteriaQuery<T> query=builder.createQuery(entityClass);
  CriteriaQuery<Long> countQuery=builder.createQuery(Long.class);
  EntityType<T> entity=entityManager.getMetamodel().entity(entityClass);
  boolean isPaged=datatablesCriterias.getDisplaySize() != null && datatablesCriterias.getDisplaySize() > 0;
  Root<T> from=query.from(entityClass);
  Predicate filterCondition=null;
  Predicate searchCondition=null;
  try {
    if (datatablesCriterias.hasOneFilteredColumn()) {
      List<Predicate> condition=new ArrayList<Predicate>();
      for (      ColumnDef column : datatablesCriterias.getColumnDefs()) {
        if (column.isFilterable() && StringUtils.isNotBlank(column.getSearch())) {
          Predicate fieldlike=getFindCondition(entity,from,column.getName(),column.getSearch(),builder,true);
          if (fieldlike != null) {
            condition.add(fieldlike);
          }
        }
      }
      if (!condition.isEmpty()) {
        filterCondition=builder.and(condition.toArray(new Predicate[]{}));
      }
    }
    String toSearch=datatablesCriterias.getSearch();
    if (StringUtils.isNotBlank(toSearch) && datatablesCriterias.hasOneFilterableColumn()) {
      List<Predicate> condition=new ArrayList<Predicate>();
      for (      ColumnDef column : datatablesCriterias.getColumnDefs()) {
        if (column.isFilterable()) {
          Predicate fieldlike=getFindCondition(entity,from,column.getName(),toSearch,builder,false);
          if (fieldlike != null) {
            condition.add(fieldlike);
          }
        }
      }
      if (!condition.isEmpty()) {
        searchCondition=builder.or(condition.toArray(new Predicate[]{}));
      }
    }
  }
 catch (  EmptyResultException e) {
    return new FindResult<T>(new ArrayList<T>(0),0,isPaged,(long)datatablesCriterias.getDisplayStart(),datatablesCriterias.getDisplaySize());
  }
  if (filterCondition != null && searchCondition != null) {
    query.where(builder.and(filterCondition,searchCondition));
    countQuery.where(builder.and(filterCondition,searchCondition));
  }
 else   if (filterCondition != null) {
    query.where(filterCondition);
    countQuery.where(filterCondition);
  }
 else   if (searchCondition != null) {
    query.where(searchCondition);
    countQuery.where(searchCondition);
  }
  if (datatablesCriterias.hasOneSortedColumn()) {
    List<Order> orderList=new ArrayList<Order>();
    for (    ColumnDef column : datatablesCriterias.getSortingColumnDefs()) {
      if (column.getSortDirection() == null) {
        continue;
      }
      Path<Object> col;
      try {
        col=from.get(column.getName());
      }
 catch (      Exception e) {
        col=null;
      }
      if (col == null) {
        LOOGER.finer("String_Node_Str".concat(column.getName()).concat("String_Node_Str"));
        continue;
      }
      if (column.getSortDirection() == SortDirection.ASC) {
        orderList.add(builder.asc(col));
      }
 else {
        orderList.add(builder.desc(col));
      }
    }
    if (!orderList.isEmpty()) {
      query.orderBy(orderList);
    }
  }
  CriteriaQuery<T> select=query.select(from);
  CriteriaQuery<Long> count=null;
  if (distinct) {
    count=countQuery.select(builder.countDistinct(countQuery.from(entityClass)));
  }
 else {
    count=countQuery.select(builder.count(countQuery.from(entityClass)));
  }
  select.distinct(distinct);
  TypedQuery<T> typedQuery=entityManager.createQuery(select);
  long startItem=0;
  int pageSize=0;
  if (isPaged) {
    pageSize=datatablesCriterias.getDisplaySize();
    typedQuery.setMaxResults(pageSize);
  }
  if (datatablesCriterias.getDisplayStart() >= 0) {
    startItem=datatablesCriterias.getDisplayStart();
    typedQuery.setFirstResult((int)startItem);
  }
  List<T> elements=typedQuery.getResultList();
  long totalResultCount=elements.size();
  if (isPaged) {
    totalResultCount=entityManager.createQuery(count).getSingleResult();
  }
  return new FindResult<T>(elements,totalResultCount,isPaged,startItem,pageSize);
}