gpt4 book ai didi

java - CriteriaQuery 返回错误结果

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:42 25 4
gpt4 key购买 nike

我的 CriteriaQuery 仅返回一条记录,dateFrom = '2019-12-10'dateTo = '2019-12-20' 应该返回两条记录我也不知道为什么。

这是我从数据库中截取的屏幕截图。

enter image description here

这是我的查询:

        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Reservation> reservationQuery = criteriaBuilder.createQuery(Reservation.class);
Root<Reservation> reservationRoot = reservationQuery.from(Reservation.class);

Predicate dateFrom = criteriaBuilder.greaterThanOrEqualTo(reservationRoot.get("dateFrom"), reservationDto.getDateFrom());
Predicate dateTo = criteriaBuilder.lessThanOrEqualTo(reservationRoot.get("dateTo"), reservationDto.getDateTo());

reservationQuery.where(dateFrom, dateTo);
TypedQuery<Reservation> res = entityManager.createQuery(reservationQuery);
List<Reservation> result = res.getResultList();

最佳答案

我通过查询解决了我的问题,它工作正常。

        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Reservation> reservationQuery = criteriaBuilder.createQuery(Reservation.class);
Root<Reservation> reservationRoot = reservationQuery.from(Reservation.class);

Predicate dateFrom = criteriaBuilder.greaterThanOrEqualTo(reservationRoot.get("dateFrom"), reservationDto.getDateFrom());
Predicate dateTo = criteriaBuilder.lessThanOrEqualTo(reservationRoot.get("dateTo"), reservationDto.getDateTo());

criteriaBuilder.and(dateFrom);
criteriaBuilder.and(dateTo);
// reservationQuery.where(dateFrom, dateTo);
TypedQuery<Reservation> res = entityManager.createQuery(reservationQuery);
List<Reservation> result = res.getResultList();
return result;

关于java - CriteriaQuery 返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59690861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com