这正是 ANSI SQL 所称的日期/时间算术,特别是您正在寻找 INTERVAL 数据类型处理。不幸的是,数据库对 INTERVAL 数据类型的支持差异很大。我真的很想在 HQL 中支持这一点(可能还有标准,尽管这取决于 JPA 规范委员会的协议)。就像我说的那样,困难在于对间隔的各种(如果有的话)支持。
目前最好的选择(通过 Hibernate 4.1)是提供一个 自定义函数 (org.hibernate.dialect.function.SQLFunction) 注册到 Dialect(搜索 google 以了解这是如何完成的)或“自定义函数注册表”(org.hibernate.cfg.Configuration#addSqlFunction)。您可能希望它以间隔呈现给特定于数据库的 date-arith 表示。
这是一个使用 Oracle NUMTODSINTERVAL 函数的示例:
public class MySqlFunction implements SQLFunction
{
public Type getReturnType(Type firstArgumentType,
Mapping mapping) throws QueryException
{
return TimestampType.INSTANCE;
}
public String render(Type firstArgumentType,
List arguments,
SessionFactoryImplementor factory) throws QueryException
{
// Arguments are already interpreted into sql variants...
final String dueDateArg = arguments.get( 0 );
final String beforeArg = arguments.get( 1 );
// Again, using the Oracle-specific NUMTODSINTERVAL
// function and using days as the unit...
return dueDateArg + " + numtodsinterval(" + beforeArg + ", 'day')";
}
public boolean hasArguments() { return true; }
public boolean hasParenthesesIfNoArguments() { return false; }
}
您可以在 HQL 中使用它,例如:
select ...
from Event e
where current_date() between e.dueDate and
interval_date_calc( e.dueDate, e.before )
其中 'interval_date_calc' 是您注册 SQLFunction 的名称。