Hello,
I tried to do a little test and wrote two classes:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Tenant {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    @Persistent
    private String name;

        public Tenant(String name){
                this.name = name;
        }

        public String getName(){
                return this.name;
        }

    public void setKey(Key key) {
        this.key = key;
    }

    public Key getKey(){
        return this.key;
    }
}

and

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Account {

        @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String name;

    @Persistent
    private String description;

    @Persistent
    private String address;
...
}

I generate a set of Accounts, which belong to a tenant with this code:

PersistenceManager pm = PMF.get().getPersistenceManager();
KeyFactory.Builder keyBuilder = new KeyFactory.Builder
(Tenant.class.getSimpleName(), "TenantX121"); Key key =
keyBuilder.getKey();
Tenant tenant = new Tenant("TenantX121");
tenant.setKey(key);
pm.makePersistent(tenant);

                for(int i = 1; i < 1001; i++){
                     keyBuilder = new 
KeyFactory.Builder(Tenant.class.getSimpleName
(), "TenantX121");
                     keyBuilder.addChild(Account.class.getSimpleName(), 
"acctid"+i);
                     Key acckey = keyBuilder.getKey();

                     Account acct = new Account(name, description,
address, ...);
                     acct.setKey(acckey);
                     pm.makePersistent(acct);
               }

Now I want to query all Accounts that belong to a tenant:

PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();

try {
  tx.begin();
   Key k = KeyFactory.createKey(Tenant.class.getSimpleName(),
"TenantX121");
   //Tenant e = pm.getObjectById(Tenant.class, k);

  Query query = pm.newQuery(Account.class);
  query.setFilter("Key == keyParam");
  query.declareParameters("Key keyParam");
  List<Account> accounts = (List<Account>)query.execute(k);

But unfortunately this doesn't work. In the documentation
http://code.google.com/appengine/docs/java/datastore/transactions.html
example they query on parent-pk, but it is not clear, what parent-pk
is (is it an attribute or a metadatafield). Of course I also tried to
query on parent-pk, but whatever I do, I receive this error:

javax.jdo.JDOException: Key
        at
org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException
(NucleusJDOHelper.java:475)
...
NestedThrowablesStackTrace:
Key
org.datanucleus.exceptions.ClassNotResolvedException: Key
        at org.datanucleus.util.Imports.resolveClassDeclaration(Imports.java:
194)

I've also tried to add
    @Persistent
    @Extension(vendorName="datanucleus", key="gae.parent-pk",
value="true")
    private Key customerKey;
to my Accountclass, but this would only be used if I would like
appengine to generate the Primarykey of the child objects itself I
suppose.
What am I doing wrong?

--

You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=.


Reply via email to