如何为hibernate3编写java条件查询。
从some_table
中选择*,其中created_at
介于DATE_SUB(curdate(),INTERVAL 8 DAY)和date_sub(curdate(),INTERVAL 2 DAY)之间
CriteriaBuilder builder = sessionFactory.getCriteriaBuilder();
CriteriaQuery<MyEntity> criteria = builder.createQuery(MyEntity.class);
Root<MyEntity> root = criteria.from(MyEntity.class);
答案 0 :(得分:1)
使用旧的java.util.Calendar
和java.util.Date
。
Calendar now = Calendar.getInstance();
now.add(Calendar.DATE, -8);
Date eightDaysAgo = now.getTime();
now.add(Calendar.DATE, 6);
Date twoDaysAgo = now.getTime();
Expression<Date> createdAt = root.<Date>get("created_at"); // choose the name of the property in Entity definition
Predicate predicate = builder.between(createdAt, twoDaysAgo, eightDaysAgo);
criteria.where(predicate);