首页 > 解决方案 > How to fetch today's date in CriteriaBuilder in hibernate

问题描述

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Star> criteriaQuery = builder.createQuery(A.class);
Root<Star> root = criteriaQuery.from(A.class);

I am already having MONDAY of the week and FRIDAY of the week and then check the today's date lies between MONDAY and FRIDAY of the week.

I have use before and after method thats not working for me.

标签: javahibernatecriteriahibernate-criteria

解决方案


Try this. use cb.currentTime() or cb.currentTimestamp()

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Star> criteriaQuery = builder.createQuery(A.class);
Root<Star> root = criteriaQuery.from(A.class);
// Add this condition
List<Predicate> conditions = new ArrayList<>();
conditions.add(cb.equal(root.get("Your  time column name"),cb.currentTimestamp()));
cq.where(conditions.toArray(new Predicate[]{}));
Query query=session.createQuery(cq);

推荐阅读