I have written a Hibernate criteria to filter and display records that have a certain Time input.
For this, my database has a single record with Date time component 12:30. I have written a Hour('HH') version of this, I feed in (int 12) for hour and it works.
Using a criteria of (int 30) for Minute doesn't return the record in the database. In fact, no matter what I input, it doesn't return. Is there something wrong with the criteria I wrote below?
public class MinEqCriteria implements Criterion {
private final String propertyName;
private final int min;
public MinEqCriteria(String propertyName, int min) {
this.propertyName = propertyName;
this.min = min;
}
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
String[] columns = criteriaQuery.findColumns(propertyName, criteria);
if (columns.length!=1) {
throw new HibernateException("minEq may only be used with single-column properties");
}
String str = "TO_CHAR(" + columns[0] + ",'mm') = ?";
return str;
}
@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[] {new TypedValue(IntegerType.INSTANCE, min, EntityMode.POJO)};
}
@Override
public String toString() {
return "min(" + propertyName + ") = " + min;
}
}
I have found the answer. Apparently in my sqldeveloper, it is indicated that the format of that data field is "HH:MI". Thus in the Critera code, I should have used the following instead:
String str = "TO_CHAR(" + columns[0] + ",'MI') = ?";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.