我如何仅获得来自给定用户的实体
public List<T> findRangeByUser(int[] range, User user) {
javax.persistence.criteria.CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
Predicate condition = cb.equal(cq.from(User.class), user);
cq.select(cq.from(entityClass));
cq.where(condition);
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
我应该怎样解决这个问题?另请注意,用户是引用User表中userId的外键 目前正在归还所有物品 非常感谢你
答案 0 :(得分:0)
条件构建器需要一个条件来评估返回结果的用户ID。像这样:
Predicate condition = qb.equal(root.get(field), usuerController.getUserSession().getUser().getIdUser());
critera.where(condition);
将“field”替换为引用用户ID的SingularAttribute。
答案 1 :(得分:0)
public List<T> findRangeByUser(int[] range, User user) {
javax.persistence.criteria.CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.where(cb.equal(rt.get("useridUser"), user));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}