views:

310

answers:

2

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...

+1  A: 

I'm sure reading of the log (Debug level) would tell you way more, since it certainly tells you when it is detaching things. Perhaps GAE/J is not respecting lazy loading at detach ? DataNucleus itself works fine, with all other datastores.

Why call FetchPlan.setGroup() when that overwrites all existing groups ? addGroup() makes more sense to me.

DataNucleus
Changing setGroup() to addGroup() totally fixed it. Thank you! Amazing how I can have almost everything right and that one little mistake cost me days... I guess its what happens with being new to a complex framework.
tempy
A: 

i have similar problem. when updating _Decks collection to a new set of decks i see that the new decks have been appended to the orignial collection. now the user has the old deck and new deck. this happens only when operating on the dettached object.

sandeep