I am able to get the interface partially working using nativeQuery=true, but I think I am missing a piece. Here is what I would like it to look like:
Code:
public interface EmailHistoryRepository extends JpaRepository<EmailHistory, Long> {
@Query(nativeQuery = true, value="SELECT TO_CHAR(email_date, 'yyyy-MM-dd') AS sentDate, COUNT(email_date) AS sentCount FROM email_history GROUP BY TO_CHAR(email_date, 'yyyy-MM-dd') ORDER BY TO_CHAR(email_date, 'yyyy-MM-dd') DESC")
public Iterable<EmailHistoryCounts> getSentCounts();
When I try this approach, I get ClassCastException - it says Object cannot be cast to the EmailHistoryCounts.
What I CAN do is change the signature to this:
Code:
public Iterable<Object[]> getSentCounts();
And then in my code that calls that method populate my EmailHistoryCounts object.
Code:
Iterable<Object[]> counts = ehr.getSentCounts();
List<EmailHistoryCounts> countArray = new ArrayList<EmailHistoryCounts>();
for (Object[] obj : counts) {
EmailHistoryCounts ehc = new EmailHistoryCounts();
ehc.setSentDate((String) obj[0]);
ehc.setSentCount((BigDecimal) obj[1]);
countArray.add(ehc);
}
Any idea on why it can't cast the results to the EmailHistoryCounts?