I want to execute this query in JPA.But i am getting error while executing this query.How to use Month(),Year() in JPA We can use these in native SQL Query.But how to use i
Dears,
sometimes we have to think with different way, maybe this can help:
TypedQuery query = entityManager.createQuery(
"SELECT SUM(COALESCE(p.amount, 0)) FROM Payment p WHERE p.paymentDate BETWEEN ?1 AND ?2", Number.class);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, month -1);
calendar.set(Calendar.YEAR, year);
query.setParameter(1, calendar.getTime());
//the location is very important
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.MILLISECOND, -1);
query.setParameter(2, calendar.getTime());
return query.getSingleResult().doubleValue();