How to store list of values in datastore

1,893 views
Skip to first unread message

Deepak Singh

unread,
Sep 14, 2011, 11:43:33 AM9/14/11
to google-a...@googlegroups.com
Hi,

I am using GAE 1.5.4 JAVA.

public class Citynames implements Serialisable{
   String id;
    String name;
   // getter setter
}

I have a list of city names contained in an arraylist,

ArrayList<String> list = new ArrayList<String>();


for(........)  { 

Citynames city = new Citynames();
city.add("11");
city.add(""ddd);
list.add(city);

}

Now i need to store this list in datastore.
What is the best way to acheive this as 

datastore.put(list) is not working and fails with following exception 

Citynames is not a supported property type.


Thanks
Deepak







Deepak Singh

unread,
Sep 14, 2011, 2:40:33 PM9/14/11
to google-a...@googlegroups.com
Anyone can help on this...

Ronoaldo José de Lana Pereira

unread,
Sep 14, 2011, 2:57:27 PM9/14/11
to google-a...@googlegroups.com
Hi Deepak,

You may be interested on the Storing Data part of the documentation.  It explains how the Datastore works and how you can use the raw Entity class to store data. This is the "low level datastore" way to persist your data:

Entity e = new Entity("Citynames");
e.setProperty("name", "Name of the City");
DatastoreServiceFactory.getDatastoreService().put(e);

This is not the usual way to store Java objects, but is the raw way. I recomend you to look at Objectify documentation, probrably the greatest persistency layer for appengine out there.

Hope this helps.

Best Regards,

-Ronoaldo

Deepak Singh

unread,
Sep 14, 2011, 3:20:58 PM9/14/11
to google-a...@googlegroups.com
Hi Ronoaldo,

This is fine. But i want to store the arraylist directly to datastore.
So 

Entity e = new Entity("Citynames");
e.setProperty("name", list);
DatastoreServiceFactory.getDatastoreService().put(e);

How is the above functionality possible?


2011/9/15 Ronoaldo José de Lana Pereira <rper...@beneficiofacil.com.br>
--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/TZVTugAw1dQJ.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Ronoaldo José de Lana Pereira

unread,
Sep 14, 2011, 3:50:54 PM9/14/11
to google-a...@googlegroups.com
Hi Deepak,

The datastore supports Collections as a property for an Entity. To the BigTable, this is a "list property", I.E, a multi-value field in one row in the datastore. It is fine to add a list of names, i.e.:

Entity e = new Entity("Citynames");
e.setProperty("name", Collections.asList("name1", "name2", "name3'));
DatastoreServiceFactory.getDatastoreService().put(e);

This will create en entry in the datastore with "Citynames" as kind, and with a property called "name" with the tree values in the list. You may find more info about what can be stored in an Entity property here, and a few notes about too many values on the same property of an entity here.

Hope this helps,

- Ronoaldo

Deepak Singh

unread,
Sep 14, 2011, 4:27:16 PM9/14/11
to google-a...@googlegroups.com
Thanks Ronoaldo. I got it now.

2011/9/15 Ronoaldo José de Lana Pereira <rper...@beneficiofacil.com.br>
Hi Deepak,

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/2L4-DpdWMuAJ.
Reply all
Reply to author
Forward
0 new messages