It looks like you are looking for a solution with JPQL to execute queries like SELECT p FROM Period p WHERE datediff(p.to, p.from) > 10 .
I am afraid that such a function does not exist in JPQL, so I recommend using your own SQL. Your idea if the Dialect extension with Hibernate SQLFunctionTemplate was very smart. I would prefer to change it to use DATE_PART('day', end - start) , as this is a way to achieve the difference between dates with PostgreSQL.
You can also define your function in PostgreSQL and use it with function() criteria .
'CREATE OR REPLACE FUNCTION "datediff"(TIMESTAMP,TIMESTAMP) RETURNS integer AS \'DATE_PART('day', $1 - $2);\' LANGUAGE sql;' cb.function("datediff", Integer.class, end, start);
source share