我有一个查询,我会写在较旧的Hibernate中(利用SessionFactory
bean)。但是,我已经转向Spring Boot,现在正在使用JPA 2,它本质上像是一个Hibernate的抽象层。任何人都可以指导我如何添加限制?我相信我现在必须在JPA中使用EntityManager
bean。这是旧的查询。用EntityManger使用JPA2重写Hibernate SessionFactory查询,涉及日期限制
@Override
@SuppressWarnings("unchecked")
public List<Party> queryPartiesBetweenDates(Date startDate, Date endDate, String sortBy, Integer count) {
Criteria criteria = getCurrentSession().createCriteria(Party.class);
if (startDate != null) {
criteria.add(Restrictions.ge("startDate", startDate));
}
if (endDate != null) {
criteria.add(Restrictions.lt("endDate", endDate));
}
if (count != null) {
criteria.setMaxResults(count);
}
if (sortBy == null || !sortBy.equals("distance")) {
criteria.addOrder(Order.asc("startDate"));
}
return criteria.list();
}
谢谢!
这将有所帮助。 https://www.jumpingbean.co.za/blogs/jpa2-criteria-api – Jobin