0
votes

I'm facing this scenario: I have three entities (EntA, EntB and EntC) EntA and EntB have the a Key< EntC> parameter. Also EntC has a List< String> parameter that holds the Keys of EntA and EntB as String.

In order to get the keys of any of the entities I first have to persist them (id field is auto-generated). So I'm doing this:

  1. Create and save EntC, this creates the Key< EntC> value I need for EntA and EntB.
  2. I create EntA and EntB using Key< EntC> and save them.
  3. Update EntC by adding Key< EntA> and Key< EntC> to the corresponding list. I found entities aren't updated in DS but re-written. So, I first fetch EntC, get its id which I use to create a new EntC entity that contains Key< EntA> and Key< EntB>. Finally, I delete the original EntC and save the new EntC.

I'd like to know if there's a better to approach to acomplish that. Also, is it possible that another entity gets the id of the original EntC entity after I delete it and before I save the new EntC entity?

1

1 Answers

0
votes

Allocate the id in advance:

For example with a parent do:

Key<YourEntity> k=  dao.fact().allocateId(parentObj, YourEntity.class);
(obviously dao extends DAOBase, I think you know that from using objectify)


 allocateId(java.lang.Class<T> clazz) 
       Allocates a single id from the allocator for the specified kind.


 allocateId(java.lang.Object parentKeyOrEntity, java.lang.Class<T> clazz) 
      Allocates a single id from the allocator for the specified kind.

http://objectify-appengine.googlecode.com/svn/trunk/javadoc/com/googlecode/objectify/ObjectifyFactory.html