Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return only entities which have a list field empty in Objectify

Tags:

objectify

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.

like image 439
iluz Avatar asked Jan 24 '26 13:01

iluz


1 Answers

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.

like image 134
stickfigure Avatar answered Jan 26 '26 13:01

stickfigure