Calendar in Criteria Querys


i am having troubles creating criteria querys for Calendar objects. As far as I can see CriteriaBuilder has no methods for checking if one Calendar is bevor or after another. Javas Calendar Class got such methods, but i dont know how to use those in my query.


JPA supports 3 temporal types:

  • java.sql.Date - representing date with no time.
  • java.sql.Time - representing time with no date.
  • java.sql.Timestamp - representing both data and time.

If possible, you should use these types instead of java.util.Calendar and java.util.Date, which eventually should be handled as one of the above value types. Particularly Calendar is not commonly used with ObjectDB and may have some limitations.

Anyway, the following example demonstrates criteria queries with Calendar. The usual comparison methods are used rather than before and after:

import java.util.Calendar;
import java.util.List;
import java.sql.Timestamp;

import javax.persistence.*;
import javax.persistence.criteria.*;

public final class T615 {

    public static void main(String[] args) {
        EntityManagerFactory emf =
        EntityManager em = emf.createEntityManager();
        em.persist(new Event());
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery query1 = cb.createQuery();
        Root e = query1.from(Event.class);;
        query1.where(cb.lessThanOrEqualTo(e.get("c1"), e.get("c2")));
        List results = em.createQuery(query1).getResultList();

        CriteriaQuery query = cb.createQuery();
        e = query.from(Event.class);;
        ParameterExpression<Timestamp> p = cb.parameter(Timestamp.class);
        query.where(cb.lessThanOrEqualTo(e.get("c1"), p));
        results = em.createQuery(query)
            .setParameter(p, Calendar.getInstance()).getResultList();


    public static final class Event {

        private long id;
        Calendar c1;
        Calendar c2;
        Event() {
            c1 = Calendar.getInstance();
            try {Thread.sleep(200); } catch (InterruptedException e) {}
            c2 = Calendar.getInstance();

Notice that Calendar values are stored in this example as Timestamp and consequently the query parameter is defined as Timestamp.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

If I am running your example I get the folling error:

Exception in thread "main" [ObjectDB 2.3.4] Unexpected exception (Error 990)
  Generated by Java HotSpot(TM) Client VM 1.7.0 (on Windows 7 6.1).
Please report this error on
com.objectdb.o.InternalException: java.lang.NullPointerException: null
at com.objectdb.o.EXR$e.c(
at com.objectdb.o.VAR.ap(
at com.objectdb.o.BCN.o(
at com.objectdb.o.BCN.UU(
at com.objectdb.o.PBI.C(
at com.objectdb.o.PBI.q(
at com.objectdb.o.OBI.Vi(
at com.objectdb.o.BQI.Vr(
at com.objectdb.o.QRM.U4(
at com.objectdb.o.MST.U4(
at com.objectdb.o.WRA.U4(
at com.objectdb.o.WSM.U4(
at com.objectdb.o.QRR.g(
at com.objectdb.o.QRR.b(
at com.objectdb.jpa.JpaQuery.getResultList(
at TestCase.main(

You are right. The parameter value had to be specified as a Timestamp:

results = em.createQuery(query)
    .setParameter(p, Calendar.getInstance().getTime()).getResultList();

Automatic conversion from Calendar to Timestamp was added in build 2.3.4_06.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login