user2839573
user2839573

Reputation: 67

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);
}

Upvotes: 0

Views: 138

Answers (3)

Dave W. Smith
Dave W. Smith

Reputation: 24966

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());
    datastore.put(entity);
}

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

Upvotes: 1

pkuhar
pkuhar

Reputation: 601

Entity entity = new Entity("YourKindName",String|Long);

Entity entity = new Entity("YourKindName",""+x);//using string as key
//or
Entity entity = new Entity("YourKindName",x);//using number as key

Upvotes: 0

alex
alex

Reputation: 301

You can create an entity by aslo giving it key_name or id:

Entity entry = new Entity("E15", key_name, roomkey);

or,

Entity entry = new Entity("E15", id, roomkey);

and later you can get the entity:

Key key = KeyFactory.createKey(roomkey, "E15", id);
Entity entry = datastore.get(key);

Upvotes: 0

Related Questions