HQL日期差异,以分钟为单位

时间:2015-12-23 14:11:05

标签: java sql hql

我有一个实体类Foo

@Entity
public class Foo {
   @Basic
   @Column("Date_From)
   Date dateFrom;
   //...
}

我需要区分date_From和当前日期,然后获取mod

在sql中它将是

DATEDIFF(MINUTE, ct.dateFrom, :date) % 120

但是HQL不支持DATEDIFF功能,我试图将其添加到方言中

registerFunction("DATEDIFF_MINUTE", new SQLFunctionTemplate(new CalendarDateType(), "DATEDIFF(minute, ?1,?2)"));

并使用它MOD(DATEDIFF_MINUTE(ct.dateFrom, :date), 120),但我得到了异常

No data type for node: org.hibernate.hql.ast.tree.MethodNode 
 +-[METHOD_CALL] MethodNode: '('
 |  +-[METHOD_NAME] IdentNode: 'DATEDIFF_MINUTE' {originalText=DATEDIFF_MINUTE}
 |  \-[EXPR_LIST] SqlNode: 'exprList'
 |     +-[DOT] DotNode: 'Foota0_.Date_From' {propertyName=dateFrom,dereferenceType=ALL,propertyPath=dateFrom,path=ct.dateFrom,tableAlias=Foota0_,className=ru.package.Foo,classAlias=ct}
 |     |  +-[ALIAS_REF] IdentNode: '(Foota0_.Date_Create, Foota0_.User_Create)' {alias=ct, className=ru.package.Foo, tableAlias=Foota0_}
 |     |  \-[IDENT] IdentNode: 'dateFrom' {originalText=dateFrom}
 |     \-[NAMED_PARAM] ParameterNode: '?' {name=date, expectedType=null}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题解决了。好像Hibernate不接受 camelCase CAPSED_TEXT 。将函数重命名为datediff_min