为什么我的jpql . getresultlist()为一个好的查询返回0行?



我昨天使用了完全相同的查询,今天工作正常,我对程序的流程做了一些更改,查询不再返回和行。

我的程序执行的第一个函数:

 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
   {
   }

}

getSumList是这样的:

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();
}

下一个函数此时无关紧要,因为没有执行任何操作,因为列表长度为零:

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);
        }

    }

检查生成的SQL和要查询的表。由于查询需要内部连接,如果其中一个表被清除,则不会返回任何结果。如果您想获得一个0计数,您需要使用外部连接语法,这在JPA中是不可能的,除非您使用对象级映射:

"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";

相关内容

  • 没有找到相关文章

最新更新