如何在JPA中查询MySQL中的DateTime?变量before是Date()对象,以毫秒为单位。我会做类似以下的事情:
@Override
@Transactional(readOnly = true)
public List<Entry> findAllBefore(Long before, Integer nEntries)
{
CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
CriteriaQuery<Entry> criteriaQuery = builder.createQuery(Entry.class);
Root root = criteriaQuery.from(Entry.class);
criteriaQuery.select( root );
// seems not to work
criteriaQuery.where( builder.lessThan( root.get( "start" ) , before ) );
TypedQuery<Entry> typedQuery = this.getEntityManager()
.createQuery(criteriaQuery)
.setMaxResults(nEntries)
return typedQuery.getResultList();
}
我希望从相应表中获取所有条目,其中DateTime列“start”位于变量'before'中的值之前。
答案 0 :(得分:0)
约会之后:
CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
CriteriaQuery<Entry> criteriaQuery = builder.createQuery(Entry.class);
List<Predicate> predicates = new ArrayList<Predicate>();
ParameterExpression<Date> param = builder.parameter(Date.class, "start");
predicates.add(builder.lessThanOrEqualTo(root.get("beforeDate"), param)); criteriaQuery.select(customer)
.where(predicates.toArray(new Predicate[]{}));
"EntityManagerVariableName".createQuery(cq).getResultList();