简体   繁体   中英

Objectify Keys-Only Query?

I want to check if cert entity exist in the database using keys-only queries. So far I'm doing:

Iterable<Key<LikeMW>> liked = ofy().load().type(LikeMW.class).filter("likedObject", postKey).filter("user", userKey).keys();

post.setLiked(liked.iterator().hasNext());

So I have 2 questions:

1 - If I use ".first().now()" after ".keys()", does it switch from "keys-only" or it'll still be a "keys-only" query?

2 - Is there a better way to check if cert entity exist using "keys-only" queries and filter?

Thank you guys!

UPDATING

@Entity
public class LikeMW {

    @Id
    private Long id;

    @JsonIgnore
    @Index
    @Load
    private Ref<UserMW> user;

    @JsonIgnore
    @Index
    private Key likedObject;

    ...
}

And one of possible liked objects...

@Entity
public class PostMW{

    @Id
    private Long id;

    @JsonIgnore
    @Load
    private Ref<UserMW> owner;

    @JsonIgnore
    @Load
    private Ref<MediaMW> media;

    ...
}

The only way to authoritatively look up whether an entity exists is to load it by key. You can certainly do a keys-only query, but it will be eventually consistent and will not guarantee that you do not create duplicates.

Given what you are trying to do, you will almost certainly be better off parenting LikeMW with the user and using the stringified likedObject as the string id. That way you can do a strongly consistent lookup and use transactions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM