我如何在 HQL 中翻译这个
select * from Customer_data where (status = 7) and
(datediff(DAY, next_check,getdate()) < 10 ) and
DATEPART(Hour,next_check) < 10
您只需以更一般的方式重构您的查询。
...
Date nextCheck = new Date(); //this is your nextCheck date parameter
Date maxDate = DateHelper.getMaxDate(nextCheck);
Query query = session.createQuery(
"from CustomerData where status = 7 and date < :maxDate");
query.setParameter("maxDate", maxDate);
List list = query.list();
...
public class DateHelper {
public static Date getMaxDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 10);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}