Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add a child object to a parent with App Engine?
    text
    copied!<p>I have two classes in an owned one-to-many relationship. The parent is Map, and the child is POI (point of interest). I am trying to add a POI to an existing Map, but I get an exception when I try to persist my changes. Here are the two classes:</p> <pre><code>@PersistenceCapable(identityType = IdentityType.APPLICATION) public class Map { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id; @Persistent(mappedBy = "map") private List&lt;Poi&gt; pois; public List&lt;Poi&gt; getPois() { return pois; } } @PersistenceCapable(identityType = IdentityType.APPLICATION) public class Poi { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Key id; @Persistent private Map map; public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } } </code></pre> <p>And here is how I am trying to use them:</p> <pre><code>PersistenceManager pm = PMF.get().getPersistenceManager(); // create a new POI Poi poi = new Poi(); // find the Map by its ID Map map = pm.getObjectById(Map.class, Long.decode(mapId)); // add the POI to the map map.getPois().add(poi); // persist! pm.makePersistent(map); pm.close(); </code></pre> <p>The line "map.getPois().add(poi);" throws an exception saying "java.lang.ClassCastException: java.lang.Long" but doesn't tell me why. If I switch it around to "poi.SetMap(map);" it just fails silently. There is no error message and nothing happens.</p> <p>Does anybody know how to correctly handle a one-to-many relationship in App Engine? Does anybody know of any good resources? <a href="http://code.google.com/appengine/docs/java/datastore/relationships.html" rel="nofollow noreferrer">Google's documentation</a> has been mildly helpful but it is really lacking.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload