如何做到“不在”? 通过使用 Hibernate 中的限制和标准?

发布于 2024-07-30 03:48:11 字数 87 浏览 3 评论 0原文

我有类别列表。 我需要一个排除 2,3 行的类别列表。 我们可以通过使用 Criteria 和 Restriction 通过 hibernate 来实现吗?

I have list of category. I need a list of category by excluding 2,3 row. Can we achieve through hibernate by using Criteria and Restriction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦亦如幻 2024-08-06 03:48:11

你的问题有点不清楚。 假设“Category”是根实体,“2,3”是 id(或类别的某些属性的值),您可以使用以下方法排除它们:

Criteria criteria = ...; // obtain criteria from somewhere, like session.createCriteria() 
criteria.add(
  Restrictions.not(
     // replace "id" below with property name, depending on what you're filtering against
    Restrictions.in("id", new long[] {2, 3})
  )
);

使用 DetachedCriteria 也可以完成相同的操作。

Your question is somewhat unclear. Assuming "Category" is a root entity and "2,3" are ids (or values of some property of the category") you can exclude them using the following:

Criteria criteria = ...; // obtain criteria from somewhere, like session.createCriteria() 
criteria.add(
  Restrictions.not(
     // replace "id" below with property name, depending on what you're filtering against
    Restrictions.in("id", new long[] {2, 3})
  )
);

Same can be done with DetachedCriteria.

老旧海报 2024-08-06 03:48:11

对于 Hibernate 5.2 版本以来的新标准:

CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder();
CriteriaQuery<Comment> criteriaQuery = criteriaBuilder.createQuery(Comment.class);

List<Long> ids = new ArrayList<>();
ids.add(2L);
ids.add(3L);

Root<Comment> root = getRoot(criteriaQuery);
Path<Object> fieldId = root.get("id");
Predicate in = fieldId.in(ids);
Predicate predicate = criteriaBuilder.not(in);

criteriaQuery
        .select(root)
        .where(predicate);

List<Comment> list = getSession()
        .createQuery(criteriaQuery)
        .getResultList();

For the new Criteria since version Hibernate 5.2:

CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder();
CriteriaQuery<Comment> criteriaQuery = criteriaBuilder.createQuery(Comment.class);

List<Long> ids = new ArrayList<>();
ids.add(2L);
ids.add(3L);

Root<Comment> root = getRoot(criteriaQuery);
Path<Object> fieldId = root.get("id");
Predicate in = fieldId.in(ids);
Predicate predicate = criteriaBuilder.not(in);

criteriaQuery
        .select(root)
        .where(predicate);

List<Comment> list = getSession()
        .createQuery(criteriaQuery)
        .getResultList();
总以为 2024-08-06 03:48:11
Session session=(Session) getEntityManager().getDelegate();
Criteria criteria=session.createCriteria(RoomMaster.class);
//restriction used or inner restriction ...
criteria.add(Restrictions.not(Restrictions.in("roomNumber",new String[] { "GA8", "GA7"})));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<RoomMaster> roomMasters=criteria.list();
Session session=(Session) getEntityManager().getDelegate();
Criteria criteria=session.createCriteria(RoomMaster.class);
//restriction used or inner restriction ...
criteria.add(Restrictions.not(Restrictions.in("roomNumber",new String[] { "GA8", "GA7"})));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<RoomMaster> roomMasters=criteria.list();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文