I was able to get it working with the HQL, and I was also able to get it working with Criterion by making my own Criterion / Expression classes like this:
Code:
import net.sf.hibernate.expression.AbstractCriterion;
import net.sf.hibernate.engine.SessionFactoryImplementor;
import net.sf.hibernate.engine.TypedValue;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.type.IntegerType;
import net.sf.hibernate.util.StringHelper;
import java.util.Map;
public class DateMonthEqExpression extends AbstractCriterion {
private final String propertyName;
private final Integer month;
DateMonthEqExpression(String propertyName, Integer month) {
this.propertyName = propertyName;
this.month = month;
}
public String toSqlString(SessionFactoryImplementor sessionFactory, Class persistentClass, String alias, Map aliasClasses) throws HibernateException {
String sql = StringHelper.join(
" and ",
StringHelper.prefix(
StringHelper.suffix( getColumns(sessionFactory, persistentClass, propertyName, alias, aliasClasses), ")} = ?" ),
"{fn MONTH(")
);
return sql;
}
public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass, Map aliasClasses) throws HibernateException {
TypedValue tv = new TypedValue(new IntegerType(), month);
return new TypedValue[] {tv};
}
public String toString() {
return propertyName + " equals " + month;
}
}
I also made a DateExpression class similar to the Expression class to easily get me my Criterion
and then I can call it like this:
Code:
crit.add(DateExpression.dateMonthEq("start", 3));
That matches any objcets with a "start" date that has a month of March.