简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM