You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查询仅关联code为3的Order的Person对象(排除code1、2)

解决方案

核心思路

要拿到符合要求的Person,需要同时满足两个逻辑:

  • 该Person必须关联至少一个code=3的Order
  • 该Person不能关联任何code=1或code=2的Order

你之前的代码只实现了第一个条件,没有排除存在其他code的情况,所以会混入不符合要求的Person。

修正后的代码

QueryBuilder<Person> builder = getBox().query();

// 确保Person关联至少一个code=3的Order
builder.linkMany(Person_.orders, Order_.code.equals(3));

// 排除所有关联了code=1或code=2的Person
builder.not().linkMany(Person_.orders, Order_.code.equals(1).or(Order_.code.equals(2)));

final Query<Person> query = builder.build()
        ..offset = page
        ..limit = limit;

List<Person> persons = query.find();

代码说明

  • 第一行linkMany筛选出有code=3订单的Person,保证基础匹配范围。
  • 第二行的not().linkMany(...)是核心修正:通过not()反转匹配逻辑,把所有存在code=1或code=2订单的Person从结果中排除,最终剩下的就是仅关联code=3订单的Person。

内容的提问来源于stack exchange,提问作者Fuxster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 20:35:38