Chris
Chris

Reputation: 31

Adding to a collection in google-appengine

So I've been stuck on this problem for about 3 weeks now. I've read this: http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html and it doesn't seem to be working out. Basically I have a persistent object, called a pool, and I need to add players into this pool. The players are also persistent objects. I have an unowned relationship between the pool and the players, where the pool has a set of player ID's (primaryKey ids...)

So things I've tried: 1) I used a Long as the primary key for both, this didn't work. 2) Tried using Key from the appengine as the primary key, and this didn't work because you can't use the implementation of Key on the client side. None of the workarounds I found work, and a data transfer object is sloppy/messy/errorprone/hackish and all kinds of bad. 3) Tried using a string as the id, like this...

@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String id;

and that didn't work either. When I try to add a string into its set, like this...

public void addPlayers(Pool pool, List<String> players) {
    PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();

    try {
        pm.currentTransaction().begin();
        Pool oldPool = pm.getObjectById(Pool.class, pool.getID());
        for(String id : players) {
            oldPool.addPlayer(id);
            System.out.println("Added id:" + id);
        }
        pm.currentTransaction().commit();
    } catch (Exception e) {
        pm.currentTransaction().rollback();
        e.printStackTrace();
    } finally  {
        pm.close();
    }

}

it will fail silently. As in...it just doesn't persist them.

Any suggestions would be great. This is for a school project, so unfortunately I'm forced to do it in GWT using JDO. My prof and TA's aren't able to help me because they aren't familiar with GWT...funny...so this is my last resort :)

Thanks!

Upvotes: 3

Views: 228

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101149

Although there are valid reasons to represent your relationship as a list on the 'one' side of the entity, a more usual approach is to have the entities on the 'many' side have a reference to the entity on the 'one' side. That is, instead of each Pool having a list of Players, each Player should have a reference to a single Pool that it belongs to.

Is there a reason you're not taking this approach? Have you tried it?

Upvotes: 1

Related Questions