Problem detaching entire object graph in GAE-J with JDO

Posted by tempy on Stack Overflow See other posts from Stack Overflow or by tempy
Published on 2010-03-08T22:57:49Z Indexed on 2010/06/01 22:23 UTC
Read the original article Hit count: 207

I am trying to load the full object graph for User, which contains a collection of decks, which then contains a collection of cards, as such: User:

@PersistenceCapable(detachable = "true") 
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE) 
@FetchGroup(name = "decks", members = { @Persistent(name = 
"_Decks") }) 
public abstract class User { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    protected Key _ID; 
    @Persistent 
    protected String _UniqueIdentifier; 
    @Persistent(mappedBy = "_Owner") 
    @Element(dependent = "true") 
    protected Set<Deck> _Decks; 
        protected User() 
    { 
    } 
} 

Each Deck has a collection of Cards, as such:

@PersistenceCapable(detachable = "true") 
@FetchGroup(name = "cards", members = { @Persistent(name = 
"_Cards") }) 
public class Deck { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key _ID; 
    @Persistent 
    String _Name; 
    @Persistent(mappedBy = "_Parent") 
    @Element(dependent = "true") 
        private Set<Card> _Cards =  new HashSet<Card>(); 
    @Persistent 
        private Set<String> _Tags = new HashSet<String>(); 
    @Persistent 
    private User _Owner; 
} 

And finally, each card:

@PersistenceCapable 
public class Card { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key _ID; 
   @Persistent 
    private Text _Question; 
    @Persistent 
    private Text _Answer; 
    @Persistent 
    private Deck _Parent; 
} 

I am trying to retrieve and then detach the entire object graph. I can see in the debugger that it loads fine, but then when I get to detaching, I can't make anything beyond the User object load. (No Decks, no Cards). At first I tried without a transaction to simply "touch" all the fields on the attached object before detaching, but that didn't help. Then I tried adding everything to the default fetch group, but that just generated warnings about GAE not supporting joins. I tried setting the fetch plan's max fetch depth to -1, but that didn't do it. Finally, I tried using FetchGroups as you can see above, and then retrieving with the following code:

    PersistenceManager pm = _pmf.getPersistenceManager(); 
                pm.setDetachAllOnCommit(true); 
                pm.getFetchPlan().setGroup("decks"); 
                pm.getFetchPlan().setGroup("cards"); 
                Transaction tx = pm.currentTransaction(); 
                Query query = null; 
            try { 
                tx.begin(); 
                        query = pm.newQuery(GoogleAccountsUser.class); //Subclass of User 
                        query.setFilter("_UniqueIdentifier == TheUser"); 
                        query.declareParameters("String TheUser"); 
                        List<User> results = (List<User>)query.execute(ID); //ID = Supplied 
parameter 
                        //TODO: Test for more than one result and throw 
                        if(results.size() == 0) 
                        { 
                                tx.commit(); 
                                return null; 
                        } 
                        else 
                        { 
                                User usr = (User)results.get(0); 
                                //usr = pm.detachCopy(usr); 
                                tx.commit(); 
                                return usr; 
                        } 
            } finally { 
                query.closeAll(); 
                    if (tx.isActive()) 
                    { 
                        tx.rollback(); 
                    } 
                pm.close(); 
            } 

This also doesn't work, and I'm running out of ideas...

© Stack Overflow or respective owner

Related posts about java

Related posts about google-app-engine