GAE: Datastore ---Manipulating Keys
REMEMBER from our overview of Datastore....
Entity = Object (loosely think of this as a row in a relational database--a "data entry")
is of a particular kind
has a key that uniquely identifies it
has one or more properties
Two entities of the same kind can have different properties
datastore entity = has one or more (name, value) pairs
- values are primitive data types
- each entity is of a named kind
Entity Methods (a selection) in Java API
see https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Entity
Method |
Meaning |
getKey() |
returns key associated with Entity |
getKind() |
returns kind as string (like idea of database table name) |
getProperty("property_name") |
returns the Property value as a String (like idea of column value) |
getProperties() |
gets all properties for this Entity as a java.util.Map<java.lang.String,java.lang.Object> |
setProperty("name", value) |
sets property (like idea of column value) |
|
returns true or false if property set |
|
Removes any property with the specified name |
equals(java.lang.Object object)
|
Two Entity objects are considered equal if they refer to the same entity |
Entity --setting Property and example in Java
Example1 -- here kind is "Book"
Entity book = new Entity("Book");
//Create Entity
Examples of setting property values of strings integers and date
book.setProperty("title", "The Grapes of Wrath");
book.setProperty("author", "John Steinbeck");
book.setProperty("copyrightYear", 1939);
Date authorBirthdate = new GregorianCalendar(1902, Calendar.FEBRUARY, 27).getTime();
book.setProperty("authorBirthdate", authorBirthdate);
Example2 -- here kind is "Employee"
Saving an Entity to the Datastore --- example in Java
Entity book = new Entity("Book");
//Create Entity
dataStore.put(book);
//store to current DataStore object
Deleting an Entity from the Datastore --- example in Java
In Java, you call the delete() method of the DatastoreService with either a single Key
or an Iterable<Key>.
Key k = book_entity.getKey(); //get the key of the entity object you want to delete
dataStore.delete(k);
|