为什么我得到只允许祖先查询内部交易错误 [英] Why do I get Only ancestor queries are allowed inside transactions error
问题描述
boolean r = ofy()。transact(new Work< Boolean>(){
@Override
public Boolean run(){
访问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;
}
});
而我得到
java.lang.IllegalArgumentException:仅在事务内允许祖先查询。
用于 get()
call。
为什么?我只在这个事务中查询访问
实体。
我在交易中这样做,因为我希望所有这些都是作为原子操作来执行的。
无法在事务内执行无祖先查询。您可以在没有交易的情况下执行此操作,也可以使用get替换查询。
您可以执行的操作最接近:
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.
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:
- Get entity with ancestor-less query without transaction. Remember key of the entity.
- Start transaction.
- Get entity via the key.
- 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.
- Change & save entity. Commit transaction.
这篇关于为什么我得到只允许祖先查询内部交易错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!