After further investigation of this issue, it seems that criteria queries work well with literal dates, including in version 2.4.5.
The following test case demonstrates it:
import java.util.*;
import javax.persistence.*;
import javax.persistence.criteria.*;
public final class T1469
{
static java.sql.Date myDate;
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2014);
cal.set(Calendar.MONTH, Calendar.JULY);
cal.set(Calendar.DATE, 11);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
myDate = new java.sql.Date(cal.getTime().getTime());
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(
"objectdb:$objectdb/db/test.tmp;drop");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new MyDateEntity());
em.getTransaction().commit();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MyDateEntity> q = cb.createQuery(MyDateEntity.class);
Root<MyDateEntity> root = q.from(MyDateEntity.class);
q.select(root);
Predicate predicate = cb.equal(root.get("date"), cb.literal(myDate));
q.where(predicate);
TypedQuery<MyDateEntity> query = em.createQuery(q);
List<MyDateEntity> resultList = query.getResultList();
System.out.println("Predicate: " + predicate);
System.out.println("Query: " + query.toString());
System.out.println("Result Size: " + resultList.size());
em.close();
emf.close();
}
@Entity
public static class MyDateEntity
{
java.sql.Date date = myDate;
}
}
The output (of the last ObjectDB build, but it works also with build 2.4.5) is:
Predicate: $1.date=2014-07-11
Query: SELECT $1 FROM MyDateEntity $1 WHERE $1.date=:$$cmlp1
Result Size: 1
The Predicate's toString is irrelevant. As can seen above in the query's toString (not shown in version 2.4.5), the date literal is replaced with a virtual parameter (which is initialized with the date literal value).
Note that the query will return 0 results if the date literal is not a pure date, i.e. if the following lines in the above test case are removed:
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)