简体   繁体   中英

Why is my jpql .getResultList() returning 0 rows for a good query

I was using the exact same query yesterday and it was working fine today I made a few changes to flow of the program and the query no longer returns and rows.

the first function that my programs goes to:

 public void prepareSummary(Date startDate , Date endDate)
{
int getStartDay = getDayFromDate(startDate);
   int getStartMonth = getMonthFromDate(startDate);
   //

   int getEndDay = getDayFromDate(endDate);
   int getEndMonth = getMonthFromDate(endDate);

   int getYear = getYearFromDate(startDate);

   if(getStartMonth <= getEndMonth)
   {
       if(getStartMonth == getEndMonth)
       {
           if(getStartDay < getEndDay)
           {    
               while(getStartDay <= getEndDay)
               {
                   Calendar cal = Calendar.getInstance();
                   cal.set( getYear, getStartMonth, getStartDay);
                   Date queryStart = getStartOfDay(cal.getTime());
                   Date queryEnd = getEndOfDay(cal.getTime());

                  List<Object[]> res = getSumList(queryStart, queryEnd);
                  doQuery(res);
               ++getStartDay;
               }


           }
           else
           {


           }

       }
       else
       {



   }


   }
   else
   {

   }


}

Here is what getSumList looks like:

public List<Object[]> getSumList(Date start, Date end) {
    String query = "";
    query += "SELECT COUNT(s) pCount,"
            + "p.nameText,"
            + "g.nameText,"
            + "t.shiftID"
            + " FROM Sheets s , GradeNames g , SpecieNames p, ShiftTimes t"
            + " WHERE s.createdLocal > :start and s.createdLocal < :end"
            + " AND s.specieNameIndex = p.nameIndex "
            + " AND s.gradeNameIndex = g.nameIndex"
            + " AND s.shiftIndex = t.shiftIndex"
            + " GROUP BY p.nameText , g.nameText , t.shiftID";
    Query q = em.createQuery(query);
    q.setParameter("start", start);
    q.setParameter("end", end);
    return q.getResultList();
}

This next function doesn't matter at this point because nothing is being executed because the list length is zero:

private void doQuery(List<Object[]> obj)
    {
         int length = obj.size();
        String grade = null;
        Long standingCount = (long) 0;

        System.out.println("Length" + length);


        for (int i = 0; i < length; ++i) {
            // HAVE A LIST OF ALL ITEMS PULLED FROM DATABASE
            Object[] tmpObj = obj.get(i);
            Long tmpCount = (Long) tmpObj[0];
            String tmpSpecieName = (String) tmpObj[1];
            Double tmpThickness = Double.parseDouble(getSpecie().getThicknessFromSpecie(tmpSpecieName));
            String tmpLength = getSpecie().getLengthFromSpecie(tmpSpecieName);
            String tmpGradeName = (String) tmpObj[2];
            String tmpShift = (String) tmpObj[3];
            tmpSpecieName = getSpecie().getSpecieFromSpecie(tmpSpecieName);
            //// END OF ALL ITEMS PULLED FROM DATABASE
            if (grade != pullGradeName(tmpGradeName) && grade != null) {

                  System.out.println("Count:" + standingCount + "Grade:" + tmpGradeName + "--" + "Specie" + tmpSpecieName + "Shift:" + tmpShift + "Thickness:" + tmpThickness + "Length:" + tmpLength + "SpecieNAme:" + tmpSpecieName);


                // do previous insert
                grade = pullGradeName(tmpGradeName);

            } else if (grade != pullGradeName(tmpGradeName) && grade == null) {
                grade = pullGradeName(tmpGradeName);
            } else if (grade == pullGradeName(tmpGradeName)) {
                standingCount = standingCount + tmpCount;
            }

            System.out.println("Count:" + tmpCount + "Grade:" + tmpGradeName + "--" + "Specie" + tmpSpecieName + "Shift:" + tmpShift + "Thickness:" + tmpThickness + "Length:" + tmpLength + "SpecieNAme:" + tmpSpecieName);

        }


    }

Check the SQL that is generated, and the tables you are querying over. As the query requires inner joins, if one of the tables was cleared, it would return no results. If you want to get a 0 count, you need to use an outer join syntax which isn't possible in JPA unless you use object level mappings:

"SELECT COUNT(s) pCount,"
            + "p.nameText,"
            + "g.nameText,"
            + "t.shiftID"
            + " FROM Sheets s outer join s.specialNameIndex p,"
            + " outer join s.gradeNameIndex g, outer join s.shiftIndex t"
            + " WHERE s.createdLocal > :start and s.createdLocal < :end"
            + " GROUP BY p.nameText , g.nameText , t.shiftID";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM