Is it possible to filter entities in objectify, by only returning entities which have a specific list field empty?
For example, if I have a Client entity, which has an attribute "address", which is of kind List, how can I return only the clients that have no address associated at all?
Is there some sort of "is null" or "is empty" filter? Something like:
ofy().load().type(Client.class).filter("address", null).list();
or
ofy().load().type(Client.class).filter("address is", null).list();
Many thanks.
List properties are represented by the absence of data in the datastore; there's no way to store or index "an empty list".
Your best bet is to create a synthetic indexed property like 'addressless' and populate it in an @OnSave method:
class Client {
...
@Index(IfTrue.class) boolean addressless;
@OnSave void updateAddressless() {
this.addressless = address.isEmpty();
}
}
You can now filter by this. The property doesn't have to have getter/setters so it can be completely hidden from the rest of your application (other than as a filter criteria).
This strategy is often handy for dealing with situations that are hard to query for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With