我有两个类拥有一对多的关系。父母是Map,孩子是POI(兴趣点)。我正在尝试将POI添加到现有地图,但是当我试图保留更改时发生异常。这里有两类:如何使用App Engine将子对象添加到父级?
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Map {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent(mappedBy = "map")
private List<Poi> pois;
public List<Poi> 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;
}
}
,这里是我怎么想使用它们:
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();
线 “map.getPois()加(POI);”抛出一个异常说“java.lang.ClassCastException:java.lang.Long”,但不告诉我为什么。如果我切换到“poi.SetMap(map);”它只是默默地失败。没有错误信息,没有任何反应。
有人知道如何正确处理App Engine中的一对多关系吗?有人知道任何好的资源吗? Google's documentation一直很有帮助,但它真的很缺乏。
POI =兴趣点,如帖子第二句所定义:) – 2009-08-02 20:49:43
另外,海报使用的是JDO,而不是JPA,它们相似但不相同。 – 2009-08-02 20:56:28