Here is my code to return a date expression based on last billed date.
private Expression<LocalDateTime> getNextMonth(Expression<LocalDateTime> lastBilledDate, Expression<Integer> dayOfMonth, CriteriaBuilder criteriaBuilder) {
// Handle potential null value in lastBilledDate
Expression<LocalDateTime> safeLastBilledDate = criteriaBuilder.coalesce(lastBilledDate, criteriaBuilder.literal(LocalDateTime.now()));
// Calculate target year and month with type safety
Expression<Integer> targetYear = criteriaBuilder.selectCase()
.when(criteriaBuilder.greaterThan(
criteriaBuilder.sum(criteriaBuilder.function("MONTH", Integer.class, safeLastBilledDate), 1), 12),
criteriaBuilder.sum(criteriaBuilder.function("YEAR", Integer.class, safeLastBilledDate), 1))
.otherwise(criteriaBuilder.function("YEAR", Integer.class, safeLastBilledDate).as(Integer.class)); // Ensure Integer type
Expression<Integer> targetMonth = criteriaBuilder.selectCase()
.when(criteriaBuilder.greaterThan(
criteriaBuilder.sum(criteriaBuilder.function("MONTH", Integer.class, safeLastBilledDate), 1), 12),
criteriaBuilder.literal(1))
.otherwise(criteriaBuilder.sum(criteriaBuilder.function("MONTH", Integer.class, safeLastBilledDate), 1).as(Integer.class)); // Ensure Integer type
return criteriaBuilder.function("DATEFROMPARTS", LocalDateTime.class,
targetYear,
targetMonth,
dayOfMonth);
}
The code to find targetMonth
& targetMonth
is getting a compile error saying :
Expression<Integer> expected, but Expression<Object> provided
I have already mentioned casting, but still not working. Can someone tell me whats going wrong here ?