-
-
Save edubriguenti/6aa7f6b85ccbb9bbd619979ee6d3e834 to your computer and use it in GitHub Desktop.
JPA Native Query with Kotlin and Spring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
class SalesOrderServiceImpl @Autowired constructor ( | |
val em: EntityManager | |
) : SalesOrderService { | |
override fun getDailySalesData(reportFilter: ReportFilter) | |
: List<SalesData> { | |
val q = em.createNativeQuery(""" | |
SELECT | |
s.date, | |
sum(sd.price) as amount | |
FROM sales s | |
left join sales_details sd | |
on s.id = sd.sales_id | |
where | |
s.date between ?1 and ?2 | |
group by s.date | |
""") | |
.setParameter(1, reportFilter.getStartDateTimestamp()) | |
.setParameter(2, reportFilter.getEndDateTimestamp()) | |
val resultList = q.getResultList().toList() | |
val result = resultList.map { | |
it -> | |
val lst = it as Array<out Any> | |
SalesData( | |
lst[0] as? BigInteger, | |
lst[1] as? BigDecimal | |
) | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment