我在拥有的一对多关系中有两个类。父级是 Map,子级是 POI(兴趣点)。我正在尝试将 POI 添加到现有地图,但当我尝试保留更改时出现异常。这是两个类:
@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().add(poi);” 抛出一个异常说“java.lang.ClassCastException:java.lang.Long”,但没有告诉我为什么。如果我将它切换到“poi.SetMap(map);” 它只是默默地失败了。没有错误消息,也没有任何反应。
有人知道如何正确处理 App Engine 中的一对多关系吗?有人知道有什么好的资源吗?谷歌的文档有点帮助,但确实缺乏。