Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SQLFunctionTemplate不应用查询参数的顺序_Java_Hibernate_Jpa - Fatal编程技术网

Java SQLFunctionTemplate不应用查询参数的顺序

Java SQLFunctionTemplate不应用查询参数的顺序,java,hibernate,jpa,Java,Hibernate,Jpa,我将问题简化为以下简单模板: //I extend the PostgreSQL dialect because I need a non-standard feature. public class MyDialect extends PostgreSQL82Dialect { public MyDialect() { //With PostgreSQL, "date2 - date1" returns the number of days between the 2 given

我将问题简化为以下简单模板:

//I extend the PostgreSQL dialect because I need a non-standard feature.
public class MyDialect extends PostgreSQL82Dialect {

  public MyDialect() {
    //With PostgreSQL, "date2 - date1" returns the number of days between the 2 given dates.
    registerFunction("date_diff", new SQLFunctionTemplate(StandardBasicTypes.LONG, " ((?2) - (?1)) "));
  }
}
在这个请求中,我注意到Hibernate并不关心
((2)-(1))中
后面的数字

因此,如果我使用:

Expression<Date> date1 = ...
Expression<Date> date2 = ...
em.getCriteriaBuilder().function("date_diff", Integer.class, date1, date2);
表达式日期1=。。。
表达式date2=。。。
em.getCriteriaBuilder()函数(“date_diff”,Integer.class,date1,date2);
调用将返回
(date1-date2)
的结果,但我预期
(date2-date1)


它是一个bug还是一个特性?给参数指定一个数字有什么意义?

我认为问题在于参数绑定。我在尝试向SQL Server注册DATE_ADD函数时遇到了类似的问题。这是我的方法调用:

registerFunction("addminutes", new TestSqlFunctionTemplate(TimeMillisType.INSTANCE, "DATEADD(MINUTE, ?2, ?1)"));
在深入研究之后,我发现问题在于如何呈现SQL字符串。render函数被传递一个要发送的参数列表,但由于参数是绑定的,它被发送一个表示绑定参数的“?”字符串列表。在我的示例中,使用带有绑定参数的查询时,render函数的输出为:

DATEADD(MINUTE, ?, ?)
它没有给出订单的指示。我正在寻找另一种解决方案,但到目前为止我还没有遇到任何问题