Nadalet
Nadalet

Reputation: 57

Query with Objectify, parent and children

I wanted to know if I can make a query to a class father and his son in the same query.

@Entity
public class Blog {
    @Getter
    @Setter
    @Id
    String id;

    @Getter
    @Setter
    int popularity;
}

@Entity
public class Post {
    @Id
    @Getter
    @Setter
    String id;

    @Getter
    @Setter
    String title;

    @Getter
    @Setter
    long published;

    @Getter
    @Setter
    String locale;

    @Getter
    @Setter
    @Load
    private Ref<Blog> parent; // or @Parent?

}

This would be possible? ->

Query list type Post > filter locale "Post" > order published "Post" > order popularity "Blog"

Thanks.

Upvotes: 1

Views: 780

Answers (3)

Zied Hamdi
Zied Hamdi

Reputation: 2660

Of course you can! do this by adding the popularity field to Post. You'll have to update that value each day or week if the Blog's popularity changed.

This repetition of the information is typical and very common in noSql databases, don't think merise: think always: "the way to get all data ready for reading (in a minimal request count)", synchronizing data updates is very common in noSql dbs loke cassandra or datastore. Try reading some documentation on the subject

@adevine you cannot say No! if you don't know

Upvotes: -1

stickfigure
stickfigure

Reputation: 13556

If you make the Ref field in Post a @Parent field, you can do an ancestor() query on the Blog and get everything (ancestor() includes the parent entity). However, you will get literally everything in that entity group unless you filter by the type ie Post).

This kind of micro-optimization is almost certainly pointless. It won't save you any money and it is unlikely to save you any noticeable latency, especially if you @Cache the Blog.

Upvotes: 0

adevine
adevine

Reputation: 1102

No, you cannot. This would be a join, and app engine does not support joins in queries. You either need to copy the data you want to filter on onto the Post entity, or else perform 2 separate queries and do the set intersection in memory.

Upvotes: 2

Related Questions