Datastore - transaction on entities with no parent

241 views
Skip to first unread message

Olivier Truemat

unread,
Oct 11, 2012, 3:43:31 AM10/11/12
to google-a...@googlegroups.com
Hi,

For a given application, I have to perform datastore transactions on entities which have no parent. Unless I misunderstood, this is not possible.

In order to avoid creating dummy entities in the datastore, I am creating a "fake ancestor" key to an entity that does not exists in the datastore.

I would have two questions related to this :

--> Is there any other solution to have transactions with no entity being the parent ?
--> Will this solution be supported in the long-term by Google ? Just would like to avoid being in a dead-end when an upgrade is rolled-out.



public class AAA {

    @Parent Key<AAA> ancestorKey; // This is a hack to make sure we can support transactions
    @Id     Long Id;
    // OTHER FIELDS
   
    public static Key<AAA> getAncestorKey() {
        return( new Key<AAA> (AAA.class, new Long("1")));
    }
}

Richard Watson

unread,
Oct 11, 2012, 5:00:42 AM10/11/12
to google-a...@googlegroups.com
Yup, that's fine. I believe the datastore will always allow keys to entities that don't exist, because it doesn't attempt to maintain referential integrity between entities.  There are no cascading deletes, say.  There's no other solution I'm aware of, and this is pretty central to the datastore so I doubt there will be a different mechanism. When you need consistency, provide an ancestor. When you don't, don't.

Just a reminder that that there's a rate limit on entities with the same ancestor, so don't use the same one if you're likely to get multiple simultaneous writes as your app grows in popularity.

Richard
Message has been deleted

Ian Marshall

unread,
Oct 11, 2012, 7:16:30 AM10/11/12
to Google App Engine
You say

"... I have to perform datastore transactions on
entities which have no parent. Unless I misunderstood, this is not
possible."

Even though, in my view, this topic should be discussed on
StackOverflow instead of here, I disagree with what you say; this
**is** possible.

My application exchanges data with the datastore using transactions
(and JDO), which can involve persistent entities that do not have an
entity parent. (As we know, every persistent entity group must contain
exactly one entity which has no entity parent.)

Olivier Truemat

unread,
Oct 11, 2012, 8:32:58 AM10/11/12
to google-a...@googlegroups.com
Richard,

Thanks for your feedback.

Ian,

Yes you are right, I should have posted this one on Stackoverflow. Thanks for letting me know, will take care for the next post.

While executing the transaction on a single entity which has no parent, I received an exception stating that only ancestor queries are allowed in transactions. Do you suggest that I am getting this exception because I am accessing the datastore through Objectify but I wouldn't have the issue going through JDO ?

Thanks,

Olivier

Jeff Schnitzer

unread,
Oct 11, 2012, 2:42:51 PM10/11/12
to google-a...@googlegroups.com
On Thu, Oct 11, 2012 at 5:32 AM, Olivier Truemat
<olivier...@gmail.com> wrote:
>
> While executing the transaction on a single entity which has no parent, I
> received an exception stating that only ancestor queries are allowed in
> transactions. Do you suggest that I am getting this exception because I am
> accessing the datastore through Objectify but I wouldn't have the issue
> going through JDO ?

The requirement that all queries in a transaction specify an ancestor
is inherent to the datastore. It doesn't matter what API you use.

Jeff

D X

unread,
Oct 11, 2012, 2:44:54 PM10/11/12
to google-a...@googlegroups.com
My understanding is an entity with no parent is essentially its own entity group.

If you're trying to use multiple entity groups within a transaction, you must use a cross-group transaction, and you're limited to 5 entity groups (ie, 5 entities) within a transaction.  Beyond that you have to restructure your entities to get within that limit.

Ian Marshall

unread,
Oct 12, 2012, 4:40:09 AM10/12/12
to Google App Engine
I see that the GAE/J documentation states

"Queries inside transactions must include ancestor filters.

Datastore transactions operate only on entities belonging to the same
entity group (descended from a common ancestor). To preserve this
restriction, all queries performed within a transaction must include
an ancestor filter specifying an ancestor in the same entity group as
the other operations in the transaction."

My application features JDO queries that fetch, within a transaction,
a persistent entity without an ancestor (because it is the sole entity
in its entity group and/or the root entity of its entity group).

One of the ways that I meet the datastore restriction above is to use

public <T> T javax.jdo.PersistenceManager.getObjectById(Class<T>
cls, Object key)

from within a transaction to obtain the root persistent entity sought.

(The key will contain the information that the entity sought has no
ancestor.)

My memory tells me that there is another way which I use; I simply
ensure that just one entity is returned from a query within a
transaction. This obviates returning entities from more than one
entity group!


On 11 Oct, 19:44, D X <drag...@gmail.com> wrote:
> My understanding is an entity with no parent is essentially its own entity
> group.
>
> If you're trying to use multiple entity groups within a transaction, you
> must use a cross-group transaction, and you're limited to 5 entity groups
> (ie, 5 entities) within a transaction.  Beyond that you have to restructure
> your entities to get within that limit.
>
>
>
>
>
>
>
> On Thursday, October 11, 2012 8:32:58 AM UTC-4, Olivier Truemat wrote:
>
> > *Richard,*
>
> > Thanks for your feedback.
>
> > *Ian,*
Reply all
Reply to author
Forward
0 new messages