Finding an object by its datetime

#1
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);
}
@Entity
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.

 

#2

It should work.

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

ObjectDB Support
#3

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.

 

#4

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

Reply