문제 설명
날짜 차이 제한 최대 절전 모드 (Date difference in days Restriction Hibernate)
저는 Hibernate 및 Criteria API를 사용하여 데이터베이스 요청을 작성하고 있습니다. 내가해야 할 일은 두 날짜 차이가 필요하고 그 날짜를 특정 숫자와 비교하는 것입니다.
예: 일반적으로 작성된 기준 제한:
criteria.add(Restrictions.eq("somProperty", someValue));
내가 필요한 것은:
criteria.add(Restrictions.ge("dateProperty1 ‑ dateProperty2", 15));
두 날짜 간의 날짜 차이가 15일 이상임을 의미합니다.
이를 달성하는 방법을 모르겠습니다. 그리고 예, 가능한 해결책을 찾기 위해 Google을 많이 찾았지만 필요한 적절한 자료를 얻지 못했습니다.
참조 솔루션
방법 1:
If you check the documentation of the Restrictions class, you will see that:
- most functions operate on a "property vs value" pair (just like in your first example)
- the remaining comparator functions operate on "property vs other property" pair
But no customized function is available for your case. So what option you have is using the sqlRestriction
, which can be used to express this condition in a native form of your DBMS. That would be a different, but much easier problem, altough clearly not as elegant as your original idea.
(by Shekhar Khairnar、Gergely Bacso)