Setting An Entity Property As A List With Gaelyk And Google Appengine

(originally posted on 2009-10-14)

As the Gaelyk site says, “Gaelyk is a lightweight Groovy toolkit for Google App Engine Java.”

Gaelyk makes it easier and Groovier to play with datastore entities, allowing the following syntax for creating and saving Entity:

import com.google.appengine.api.datastore.Entity

//create the entity
Entity student = new Entity("person")

//set and populate properties
student["name"] = "John Doe"
student["grade"] = "8"

student.save()

What you can’t do with this easy syntax is save a property as a list; the following line fails:

//failed attempt to make the property a list
student["colors"] = ["blue", "green"]

Instead, you have reach further down to the Entity class API:

import com.google.appengine.api.datastore.Entity

//create the entity
Entity student = new Entity("person")

//set and populate properties
student["name"] = "John Doe"
student["grade"] = "8"

//make the property a list first
student.setProperty("colors", [])
student["colors"] = ["blue", "green"]

student.save()