Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store List<Key> as property of a Entity in Google App Engine (GAE)?

    List<Key> items = new ArrayList<Key>();
    user.setProperty("ItemsList", items);

The code given above does not work. What are the ways in which this can be achieved using Java platform in GAE ?

P.S. Though the following piece of code runs fine -

List<String> items = new ArrayList<String>();
user.setProperty("ItemsList", items);
like image 894
Yash Agarwal Avatar asked Oct 24 '25 18:10

Yash Agarwal


2 Answers

It should work without any problems. Did you forget to put the item? If you didn't set id for the item, the DataStore will automatically assign the entity an integer numeric ID. You need to put that generated key into the list. Here is the sample code:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity item = new Entity("Item");
datastore.put(item);        

Entity user = new Entity("User");
List<Key> items = new ArrayList<Key>();
items.add(item.getKey());
user.setProperty("ItemsList", items);
datastore.put(user);

Please check the doc: https://developers.google.com/appengine/docs/java/datastore/entities

And, it is better to handle data layer via some third part API like Objectify: http://code.google.com/p/objectify-appengine/

like image 180
Evan Avatar answered Oct 26 '25 08:10

Evan


It should work.

Infact, that's one of the ways used to implement many-to-many and many-to-one relationships.

See, for example, http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Multi-Value_Relationship

Key used in the Objectify example is com.googlecode.objectify.Key<T> though.

like image 26
gvaish Avatar answered Oct 26 '25 08:10

gvaish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!