Tim Norman
Tim Norman

Reputation: 431

My hibernate code to retrieve records does not work

I am using the following code to retrieve all the records that their time column is passed but it does not show anything.

                Criteria cre = session.createCriteria(Timing.class, "timing")
                        .add(Restrictions.le("timing.time", getCurrentTime()));
                List<Timing> timing = new ArrayList();
                if (cre.list().size() > 1) {
                    timing = (List<Timing>) cre.list();
                }
                tx.commit();
                System.err.println("Time is:" + timing.get(0).getTime()); 
                                               <<it does not reach to this line

 }

 public Date getCurrentTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date();
        System.out.println("current:" + dateFormat.format(date));
        return date;
    }

Timing

@Temporal (javax.persistence.TemporalType.TIME)
    public Date getTime() {
        return time;
    }

Upvotes: 1

Views: 65

Answers (2)

J888
J888

Reputation: 1964

The code seems to be correct, check the data and its format on your database

Upvotes: 0

Alex
Alex

Reputation: 11579

Try this if you need to set list NULL for empty result.

List<Timing> timing = cre.list();
if (timing!=null && timing.isEmpty())
timing = null;

And I believe it should be 0 instead of 1 in the line ...size() > 1 for your current code and checking for null.

Upvotes: 1

Related Questions