jluiz20 jluiz20 - 2 months ago 16
Java Question

Filter objects passing a property of a Ref<?> of other object in GAE Datastore

I have tried different types and read the documentation and I could not figure out filtering for a property insider Ref<> is possible.

Here is my classes:
My Beer.class

@Entity
public class Beer {

@Id
private Long keyBeer;

@Load
@Index
Ref<BeerBrand> beerBrandRef;
}


And this is the Item.class

@Entity
public class Item {

@Index
@Load
Ref<Beer> beerRef;
..Fields
}


What I'm trying to do is to filter an item by
beerBrandRef
. For example, for a specific brand I need to list all the item that have that
beerBrandRef
.

This is my endpoint code:

First I load the beerBrandRef from the Key that I have:

BeerBrand tmpBrand = ofy().load().type(BeerBrand.class)
.id(pBrandKey).now();


Then I'm trying to pass the Ref in the Filter:

items = ofy().load().type(Item.class)
.filter("beerRef.beerBrandRef", tmpBrand)
.list();


Is it possible? If not, how can I structure my Datastore to get this kind of search to work(not necessarily with Ref<>)

Ps: This is my first project with the GAE, Datastore and Objectify, so I am a little bit lost.

I appreciate any help.

Answer

With respect to queries, you can treat Key<?>, Key (the native low level api version), Ref<?>, and the actual entity itself as interchangeable. You can pass any of those four things to your filter() call.

Update: You are correct - you cannot express a join in GAE queries. Which is not to say you can't do joins, you just have to do them yourself by hand in your own code - you're the query planner. The same rule also applies to aggregations. For simple joins and aggregations this works fine, but of course it has limits.

The datastore is great as a transactional store and authoritative source of truth but it is terrible for analytics. My advice is to replicate relevant bits of data into a real RDMBS, either Cloud SQL or Postgres (whose driver has been working for me on GAE out of the box). Use the task queue. This works quite well for me and provides best-of-both-worlds - the infinite scaling and zero maintenance of the datastore, plus the flexibility of an RDBMS.

Comments