Tôi đang tính toán một SUM nhóm lại theo thángJPA/Hibernate trở BigDecimal không lâu
Query q = entityManager.createNativeQuery(qlString);
q.setParameter("program", program);
@SuppressWarnings("unchecked")
List<Long> resultList = (List<Long>) q.getResultList();
long tend = System.currentTimeMillis();
Khi tôi vượt qua trong hai resultsLists (đóng cửa: ResultsList các mặt hàng đóng, closedLate: ResultsList các mặt hàng đóng muộn) vào một phương pháp tính tỷ lệ phần trăm, tôi nhận được
javax.servlet.ServletException: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long
.
private List<Long> computeOTR(List<Long> closed, List<Long> closedLate) {
List<Long> monthlyOTR = new ArrayList<Long>();
long numerator;
Long denominator;
for (int i = 0; i <11; i++) {
numerator = closed.get(i) - closedLate.get(i); <----java.lang.ClassCastException
denominator = closed.get(i);
long percentage = (int)(numerator * 100.0/denominator + 0.5);
monthlyOTR.add(i, percentage);
}
return monthlyOTR;
}
Trong chế độ debug Eclipse đóng được hiển thị như BigDecimal. Tại sao điều này khi tôi decalre
List<Long> resultList = (List<Long>) q.getResultList();
EDIT-Hibernate Query:
public List<Long> findClosedLateByProgram(String program) {
long tstart = System.currentTimeMillis();
//@formatter:off
String qlString = "with PRJ as (" +
"select trunc(END_DATE) as END_DATE, " +
"trunc(NEED_DATE) as NEED_DATE " +
"from (SELECT UNIQUE * FROM TEST where PROGRAM_NAME = :program " +
"AND ACTION_BY_ORG = 'AAA') " +
"), " +
"DATES as (select add_months(trunc(last_day(SYSDATE)), level-7) as thedate " +
"from dual connect by level <= 12 ) " +
"SELECT nvl(sum(case when NEED_DATE < trunc(thedate,'mm') AND END_DATE between trunc(thedate,'mm') and thedate then 1 end), 0) as CLOSED_LATE " +
"FROM DATES, PRJ " +
"GROUP BY thedate ORDER BY thedate";
//@formatter:on
Query q = entityManager.createNativeQuery(qlString);
q.setParameter("program", program);
// q.setParameter("today",date, TemporalType.DATE);
@SuppressWarnings("unchecked")
List<Long> resultList = q.getResultList();
long tend = System.currentTimeMillis();
long elapsed = tend-tstart;
System.out.println("Elapsed Time For Closed But Late: " + elapsed);
return resultList;
}
EDIT 2
Tôi nghĩ rằng tôi đang bị mắc kẹt với một BigDecimal? http://weblogs.java.net/blog/mb124283/archive/2007/04/java_persistenc.html
Xem thêm http://stackoverflow.com/questions/3574029/what-does-jpa-entitymanager-getsingleresult-return-for -a-count-query – Vadzim