2015-10-06 94 views
0

我昨天使用完全相同的查詢,今天工作正常我對程序流進行了一些更改,並且查詢不再返回並且行。爲什麼我的jpql .getResultList()返回0行爲一個好的查詢

,我的計劃去的第一個函數:

這裏是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); 

     } 


    } 
+0

好了,事實證明這是工作昨天並不意味着它必須今天工作。所有破碎的東西都在工作。只需複製您的SQL查詢,手動使用任何數據庫客戶端執行它,並檢查它是否會返回您所期望的。通過這種方式,您可以決定是否SQL查詢或數據損壞或Java代碼損壞。 –

回答

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"; 
相關問題