GAE : Tidbits on datastore

Google App Engine Datastore

Datastore is used to store Entities. Entities belong to a Kind, which is used to group related entities.

Kind  is like a Table in relation database

Entity is like a row in relational database.

Each Entity has a Key which is unique across all entities.

General components of a Key :  {Name of the Kind} + {App assigned String or a number}

How a Key is generated

Get handle to the  DatastoreService

DatastoreService ds = DatastoreServiceFactory.getDatastoreService;

Create the Key

<pre>

Key userKey = KeyFactory.createKey("UserPrefs",user.getUserId());
//Here "UserPrefs" is the Kind
&lt;/pre

To fetch the Entity based on a key

// This will return a UserPrefs Entity matching the userKey.
userPrefs=ds.get(userKey);

How to create a new Entity

[1] Get the handled to the DatastoreService
[2] Create  the Key
[3] Create a new Entity using the Key :

Entity user = new Entity(userKey)

[4] Set properties for the Entity

user.setProperty("firstName" , "Hello");
user.setProperty("secondName" , "World");

[5] Persist the Entity

ds.put( user)
Posted in: GAE

Leave a Comment