为什么我的jpql .getResultList()返回0行以获得良好的查询

时间:2015-10-06 17:28:08

标签: java mysql jpa netbeans jpql

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

我的程序的第一个功能:

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

        }


    }

1 个答案:

答案 0 :(得分:0)

检查生成的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";