views:

325

answers:

2

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:

@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;
    }
}

And here is how I am trying to use them:

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

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.

Does anybody know how to correctly handle a one-to-many relationship in App Engine? Does anybody know of any good resources? Google's documentation has been mildly helpful but it is really lacking.

A: 

I'm not sure that your Map is the best abstraction here. And I don't know what Poi is. Google tells me it's Hawaiian food.

But Google also shows me how to do a one-to-many unidirectional relationship with JDO. I don't see any one to many annotations in your code. Is that what you're trying to do? If so, perhaps that's what you're missing.

duffymo
POI = point of interest, as defined in the 2nd sentence of the post :)
Peter Recore
Also, the poster is using JDO, not JPA, which are similar but not the same.
Peter Recore
I'd type it out: PointOfInterest is more self-documenting that POI.
duffymo
A: 

The first thing to check is that you are using version 1.2.2 of the sdk. Many fixes/enhancements were made to child/parent functionality in this release. One issue that was fixed sounds a lot like your issue, or at least related.

If that doesn't fix the problem, check that the class you are using the above code in is annotated @PersistenceAware, assuming it is not already @PersistenceCapable.

If THAT doesn't fix the problem, can you post the full class that is causing the problem (preferably cut down to the bare minimum of code that will reproduce the exception) In your excerpt for example, we don't see the original map get created and saved, and don't get to see where mapId comes from.

Peter Recore
Updating to the latest version of the SDK worked perfectly! I was on 1.2.1 from a just few weeks ago - it would have taken me forever to figure that out.
dnorcott