Im creating a hibernate criteria that will check if a contract is/has been valid during a year. The contract has a start and end-date as a standard date-field.
So being used to SQL-SERVER, i would think something in the lines of this SQL:
SELECT * FROM CONTRACT WHERE YEAR(startDate) <= ? AND YEAR(endDate) >= ?
The questionmark is the given year as an integer.
So how to transform this to a hibernate criteria?
int year = 2011; //Just a testyear for example purposes.
DetachedCriteria criteria = DetachedCriteria.forClass(Contract.class)
.add(Restrictions.le("startDate", year))
.add(Restrictions.ge("endDate", year));
As you can see, i am missing the bit where i convert the date’s to a year so it can be compared. Naturally my code does not run as it is.
Any suggestion on how to get the desired results?
It seem like the API does not directly support what you wanted, but there is workaround that you might consider.
Solution 1:
Example output of the two methods:
Solution 2: