Using JPA/Hibernate Criteria to pull between a date

前端 未结 4 1297
清歌不尽
清歌不尽 2020-12-08 06:02

I am trying to using the following code to pull a list of Experience objects from a MySQL table. Each experience has a from datetime column and a t

4条回答
  •  时光取名叫无心
    2020-12-08 06:29

    You're just missing the call to CriteriaBuilder.literal():

    Date currentDate = new Date();
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery(Experience.class);
    Root root = query.from(Experience.class);
    builder.between(builder.literal(currentDate), root.get("from"), root.get("to"));
    return entityManager.createQuery(query).getResultList();
    

提交回复
热议问题