views:

21

answers:

1

In the following code example how do the user/userParam references relate to the Customer and Account lookups and what is the relationship between Customer and Account?

    // PersistenceManager pm = ...;
    Transaction tx = pm.currentTransaction();
    User user = userService.currentUser();
    List<Account> accounts = new ArrayList<Account>();

    try {
        tx.begin();

        Query query = pm.newQuery("select from Customer " +
                                  "where user == userParam " +
                                  "parameters User userParam");
        List<Customer> customers = (List<Customer>)
        query.execute(user);

        query = pm.newQuery("select from Account " +
                            "where parent-pk == keyParam " +
                            "parameters Key keyParam");
        for (Customer customer : customers) {
            accounts.addAll((List<Account>)
            query.execute(customer.key));
        }

    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
A: 

The first query is querying all Customers where Customer.user == userParam. So it looks like Customers have a one-to-one relationship with Users (presumably this is the GAE User class).

The second query is querying all Accounts where the account's parent-key is the key of a customer. So it looks like Customers have a parent-child relationship with Accounts.

Putting it all together, every User has one Customer, and every Customer can have multiple child Accounts. An Account can only have one parent Customer.

Jason Hall