title: JPA Criteria API - arithmetic with Calendar values
body: <p>I am rewriting a huge search query, originally created in Hibernate Criteria. I am trying to use only standard JPA solutions. The problem I am facing with is, that I want to make a where clause like this:</p>
  
  <pre><code>select ... where (stopTime - startTime) &gt; minimalLength
  </code></pre>
  
  <p>And this does not seems to possible with Calendar values, as I can't do arithmetic with them using Criteria API. Is there a solution for this?</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>As it was mentioned in the answers (now it is deleted), Criteria API has the <code>sum</code>, <code>diff</code>, <code>prod</code> methods, but these have the signature of :</p>
  
  <pre><code>&lt;N extends java.lang.Number&gt; Expression&lt;N&gt;
      method(Expression&lt;? extends N&gt; x, Expression&lt;? extends N&gt; y)
  &lt;N extends java.lang.Number&gt; Expression&lt;N&gt;
      method(N x, Expression&lt;? extends N&gt; y)
  &lt;N extends java.lang.Number&gt; Expression&lt;N&gt;
      method(Expression&lt;? extends N&gt; x, N y)
  </code></pre>
  
  <p>So they are not usable for <code>Calendar</code> (or even for <code>Date</code>).</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>I think, I am on the right track. I decided to use the <code>function</code> method of <em>CriteriaBuilder</em>, and use it to call the (unfortunately vendor specific) <code>datediff</code> SQL Server method. It looks like this:</p>
  
  <pre><code>builder.function("datediff", Integer.class, builder.literal("second"), startTime, stopTime)
  </code></pre>
  
  <p>This <em>nearly</em> works, the only problem is, that <code>datediff</code> does not work with <em>String</em> as the first parameter, it needs a keyword (i.e. "second" without quotes). Does anybody know a way to pass a literal to the function?</p>