GaryHost GaryHost - 1 month ago 16
Java Question

Objectify query amongst embedded Ref list

In my datastore, I have a post entity, which has a sender, and this sender have a list of locations. Basically, this looks like this :

@Entity
@Cache
@Unindex
@Searchable
public class Post {
@Index
@Load(WithSender.class)
@NonNull @Getter @Setter
protected Ref<User> sender;


@Index
@Getter
protected long creation;
}

@Entity
@Cache
@Unindex
public class User {

@Index
@NotNull
@Email
@NonNull
@Getter
@Setter
private String email;

@Size(max = 50)
@Getter
@Setter
private String firstName;

@Size(max = 50)
@Getter
@Setter
private String lastName;

@Index
@Setter
@Getter
protected List<Key<Region>> regions;
}


What I want to do, is being able to query on all posts that had been sent by a user belonging to a Region that is in the list of Regions of this user, and by order of creation.

Basically, I tried doing this :

query = query.filter("sender.regions", "key").order("-creation");


When I executed the query the first time, AppEngine requested that I create this index :

<datastore-index kind="Post" ancestor="false" source="manual">
<property name="sender.regions" direction="asc"/>
<property name="creation" direction="desc"/>
</datastore-index>


But the results remains hopelessly empty... If anyone has a suggestion, I'll be glad. Thanks !

Answer

In order to query a Post by "sender.regions" the Sender entity should be embedded.

There is no SQL like "join" in datastore. You either embed the sender on the post or query for Sender keys first by regions and then query for Posts with those sender keys.

Hope it helps.

Comments