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

在ObjectBox中使用QueryBuilder能否对比同一实体的两个属性?

Answer to Your Query Comparison Question

Great question! Let's break this down clearly for you.

1. Directly Comparing Two Fields in the Same Entity (Your Core Need)

First off, your goal to replicate the SQL SELECT * FROM myTable WHERE currentSteps < totalSteps; is totally achievable—you just don't want to use the less() method that accepts a fixed value (like your first example with 5).

Most modern ORM or query frameworks support comparing two properties from the same entity. For example, if you're using JPA's Criteria API (a common fit for this style of querying), you'd write something like this:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<BasicAchievement> query = cb.createQuery(BasicAchievement.class);
Root<BasicAchievement> root = query.from(BasicAchievement.class);

// Compare currentSteps directly to totalSteps on the same entity
query.select(root)
     .where(cb.lessThan(root.get(BasicAchievement_.currentSteps), root.get(BasicAchievement_.totalSteps)));

List<BasicAchievement> results = entityManager.createQuery(query).getResultList();

If your myBox.query() chain is a custom framework or a wrapper around something like this, look for a method that accepts two attribute references instead of an attribute and a literal. It might be named something like lessProperty(), compareLess(), or you might need to pass a predicate builder that handles the attribute-to-attribute comparison.

2. Do You Need to Join Tables?

For your specific use case (comparing two fields in the same table/entity), you don't need to join any tables. That SQL-style thinking is totally understandable, but since both currentSteps and totalSteps live on the same BasicAchievement entity, no joins are required.

That said, if you ever did need to compare fields across related entities (e.g., BasicAchievement.currentSteps vs User.maxAllowedSteps), most frameworks do support joining associated entities and comparing their fields—similar to how you'd use JOIN in SQL.

Final Note

Your intuition was right: this is absolutely a supported operation. The key is to use your framework's method for attribute-to-attribute comparisons, rather than the method designed for attribute-to-literal comparisons.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:01:57