title: JPA CriteriaQuery calculating datetime difference for use in where predicate
body: <p>I am trying to select entities from my database that have a time difference between 2 fields less than or greater to some value. In standard SQL, this is simply accomplished using the TIMESTAMPDIFF function:</p>
  
  <pre><code>SELECT * from run where TIMESTAMPDIFF(SECOND, run.end_time, run.start_time) &gt; 60;
  </code></pre>
  
  <p>However, there doesn't seem to be any equivalent in JPA 2.0. </p>
  
  <p>I've tried everything I can think of, including the suggestions here: <a href="https://stackoverflow.com/questions/22412234/using-timestampdiff-with-jpa-criteria-query-and-hibernate-as-the-provider">Using TIMESTAMPDIFF with JPA criteria query and hibernate as the provider</a></p>
  
  <pre><code>predicate.getExpressions().add(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("TIMESTAMPDIFF", Long.class, criteriaBuilder.literal("SECOND"), root.get(Run_.endTime), root.get(Run_.startTime)), minSeconds))
  </code></pre>
  
  <p>simply doesn't work and since I am specifically trying to calculate large durations (60/90 days). The TIMEDIFF solution doesn't help because the max TIMEDIFF that can be returned is 35 days. Is there some other way to accomplish this?</p>