-1

我使用的条件查询如下:我想在我过去的时间和日期之后获取预订。但现在我在同一日期过去的时间之前获得预订。请帮助..

 @GET
        @Path("/getReservationByUserIdAndTypeAndDateTime/{userid}/{type}/{date}/{time}")
        public List<Reservation> getReservationByAndUserIdAndTypeAndDateTime(@PathParam("userid") int uid, @PathParam("type") int tid,@PathParam("date") Date date,@PathParam("time") Time time) {
            CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Reservation> criteria = builder.createQuery(Reservation.class);
            Root<Reservation> r = criteria.from(Reservation.class);
            TypedQuery<Reservation> query = em.createQuery(
                criteria.select(r).where(new Predicate[]{
                        builder.equal(r.get(Reservation_.usersId), uid), 
                        builder.equal(r.get(Reservation_.reservationsType), tid),

                        builder.greaterThanOrEqualTo(builder.concat(r.get(Reservation_.date), " "+r.get(Reservation_.time)), date.toString()+" "+time.toString())}));
            return query.getResultList();
        }
4

1 回答 1

1

不建议比较格式化为字符串的日期和时间,结果可能取决于格式。最好Date在班级中有一个独特的领域Reservation

@Temporal(TIMESTAMP) Date date;

并在查询中使用日期比较:

 builder.greaterThanOrEqualTo(r.get(Reservation_.date), date)

这样可以省略方法中的time参数,因为该参数将包含日期和时间。getReservationByAndUserIdAndTypeAndDateTimedate

于 2013-01-17T09:52:13.287 回答