I have these 2 entities in objectbox:
@Entity()
class Account {
@Id()
int id = 0;
}
@Entity()
class Transaction {
@Id()
int id = 0;
final fromAccount = ToOne<Account>();
final toAccount = ToOne<Account>();
}
I need to query all transactions from or to a particular account. I’m unable to figure out the query. Tried this but it doesn’t work:
Future<List<Transaction>> getAllTransactionsForAccount(int accountId) {
QueryBuilder<Transaction> builder = box.query();
builder.link(
Transaction_.toAccount, Account_.id.equals(accountId));
builder.link(
Transaction_.fromAccount, Account_.id.equals(accountId));
Query<Transaction> q = builder.build();
return q.findAsync();
}
If I remove one of the builder.link
for fromAccount
or toAccount
, it works, but I need both, not just one.
Could someone help please ?
2
Answers
the code you have given will allow you to apply one condition at a time, and to query all transactions either from or to a particular ObjectBox u can combine two conditions using or operator,
this would work
link
method in ObjectBox’sQueryBuilder
works as an"AND" operator, not an "OR"
. This means that your current query is looking for transactions where bothfromAccount
andtoAccount
are the same, which is not what you want.