我有一个拥有一对多关系的两个班级。父亲是地图,孩子是POI(兴趣点)。我正在尝试将POI添加到现有Map中,但是当我尝试保留更改时出现异常。以下是两个类:

@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();

行<!> quot; map.getPois()。add(poi); <!> quot;抛出异常说<!> quot; java.lang.ClassCastException:java.lang.Long <!> quot;但不告诉我为什么。如果我将它切换到<!>“poi.SetMap(map); <!>”;它只是默默地失败了。没有错误消息,也没有任何反应。

有人知道如何正确处理App Engine中的一对多关系吗?有人知道有什么好的资源吗? Google的文档一直很有帮助,但确实缺乏。

有帮助吗?

解决方案

要检查的第一件事是您使用的是sdk的1.2.2版。在此发布中,对子/父功能进行了许多修复/增强。 一个

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top