Question

So I'm creating user accounts in GAE's data store (using Java)...

Entity account = new Entity(DATASTORE_KIND, accountAncestorKey);  
Key accountKey = datastore.put(account);

The entity is created, (eventually), great! Now when is the object's ID available? Might it take "a little while" before I can do getProperty("ID") on the object?

If I need the ID immediately, should I manually allocate 1 ID for myself to use?

I am asking this question because I can run some code tests myself, but that does not conclusively solve the problem of whether the ID is generated "immediately" after the put... and from what I have read, I suspect I should not rely on this.

Était-ce utile?

La solution

When entity is saved it contains full key and ID is part of key. So it's available immediately when entity is available.

Note that entity ID is not a property on entity (except if you create one yourself). So uou should instead use entity.getKey().getId().

Autres conseils

I believe it should be very quick (30-50 ms) and in your case if you are trying to get the entity from the datastore right after storing it, it shouldn't be a problem.

You can use log4j and try to output the time to persist timing to get understanding of how long it would take.

Example,

INFO: Time to persist datastore: 38 ms

# A default log4j configuration for log4j users.
#
# To use this configuration, deploy it into your application's WEB-INF/classes
# directory.  You are also encouraged to edit it as you like.

# Root logger option
log4j.rootLogger=INFO

# Configure the console as our one appender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n

# tighten logging on the DataNucleus Categories
log4j.category.DataNucleus.JDO=WARN, A1
log4j.category.DataNucleus.Persistence=WARN, A1
log4j.category.DataNucleus.Cache=WARN, A1
log4j.category.DataNucleus.MetaData=WARN, A1
log4j.category.DataNucleus.General=WARN, A1
log4j.category.DataNucleus.Utility=WARN, A1
log4j.category.DataNucleus.Transaction=WARN, A1
log4j.category.DataNucleus.Datastore=WARN, A1
log4j.category.DataNucleus.ClassLoading=WARN, A1
log4j.category.DataNucleus.Plugin=WARN, A1
log4j.category.DataNucleus.ValueGeneration=WARN, A1
log4j.category.DataNucleus.Enhancer=WARN, A1
log4j.category.DataNucleus.SchemaTool=WARN, A1
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top