1

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;
    }
4

2 回答 2

1

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.

于 2013-08-15T00:37:42.647 回答
0

代码似乎是正确的,请检查数据库中的数据及其格式

于 2013-08-15T00:43:30.967 回答