Finding an object by its datetime

SearchBookingByTimeStamp() {

    Calendar btime = Calendar.getInstance();
    btime.set(2012, 10, 11, 7, 30, 00); //This time is already inserted, trying to find it

    /* My Query */
    TypedQuery<Booking> query = em.createQuery(
        "SELECT b FROM Booking b WHERE b.btime = :btime", Booking.class)
        .setParameter("btime", btime, TemporalType.TIMESTAMP);
public class Booking {
    @Temporal(TemporalType.TIMESTAMP) Calendar btime;


I'm trying to search for the exact timestamp I persisted. This is a snippet of what I'm trying accomplish (won't compile of course). My resultlist from my query gets zero results. I want to search for objects by exact timestamps.



It should work.

Could you please send a complete runnable example, as described in the posting instructions?

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

Hi, I forgot the @Index beside @Temporal(TemporalType.TIMESTAMP) Calendar btime;

It looks like this now in the entity class:

@Index @Temporal(TemporalType.TIMESTAMP) Calendar btime;

And I found that single result which is great!

Thank you for confirming that the query looks legitimate.



Interesting. It should work also with no index but you may have to define the field as Timestamp or Date rather than as Calendar (i.e. the persistent field and the parameter in the query should have the same type).

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

Post Reply

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