user2839573 user2839573 - 1 year ago 88
Java Question

Creating Entities in Google App Engine

Not sure if this is possible, but it's always worth asking.

I've simplified the problem below - basically, I want to use a for loop to create multiple entities of the same kind. The problem seems to be that I can't name a new entity by calling a variable.

Can anybody think of a way round this problem? Any help would be much appreciated.

Many thanks

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key roomkey = KeyFactory.createKey("E15", "ids");

String test = "";
for (int x = 0; x < 7; x++) {
test = ("" + Integer.toString(x));
Entity test = new Entity("E15", roomkey);

Answer Source

Assuming you are trying to create 7 entities of type E15 with key names "0" through "6"

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
for (Integer n = 0 ; n < 7 ; ++n ) {
    Entity entity = new Entity("E15", n.toString());

The last bit is important. Merely creating an instance of Entity doesn't persist it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download