Why do I get Only ancestor queries are allowed ins

2019-05-02 21:55发布

boolean r = ofy().transact(new Work<Boolean>() {

        @Override
        public Boolean run() {
            Visit visit = ofy().load().type(Visit.class)
                    .filter(Visit.USER_ID, userID)
                    .filter(Visit.VENUE_ID, venueID).first().get();

            if (visit == null)
                return false;

            visit.setLastRequestDate(new Date(timestamp));

            ofy().save().entity(visit).now();
            return true;
        }
    });

and I get

java.lang.IllegalArgumentException: Only ancestor queries are allowed inside transactions.

for the line with the get() call.
why? I'm only querying Visit entity in this transaction. I'm doing this in a transaction, because I want all this to be performed as atom operation.

2条回答
对你真心纯属浪费
2楼-- · 2019-05-02 22:22

The error seems quite clear: inside a transaction, you're only allowed to perform ancestor queries. Your query is not an ancestor query.

查看更多
该账号已被封号
3楼-- · 2019-05-02 22:26

No way to do ancestor-less query inside a transaction. Either you do it without transactions or replace query with get.

The closest that you can do is:

  1. Get entity with ancestor-less query without transaction. Remember key of the entity.
  2. Start transaction.
  3. Get entity via the key.
  4. Check that query condition still applies (= properties still have the same values as in query conditions). This way you can be sure entity was not changed since you did the query.
  5. Change & save entity. Commit transaction.
查看更多
登录 后发表回答