views:

55

answers:

1

I've been following the google app engine tutorial and the part that explains JDO is done under the basis of a guestbook. So when they query the persistence (BigTable i believe) they are interested in returning all the results.

I'm trying to adapt this for showing the results for the specific user but seem to be having trouble with it.

UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if(user != null) {
    PersistenceManager pm = PerManFac.get().getPersistenceManager();
    String query = "select * from " + Team.class.getName();
    List<Team> teamList = (List<Team>) pm.newQuery(query).execute();
    if(teamList.isEmpty()) {

Thats part of what i have so far, i need to adapt my query String to be 'where User = user' but i get issues each time.

My Team object only holds Key, User, a String and Date.

+1  A: 

You can do this without JDOQL. If a user only has one team, then you can model this with an owned one to many relationship which is bidirectional.

@PersistenceCapable
public class Team {
  @Persistent(mappedBy = "team")
  private List<Employee> employees;
}

@PersistenceCapable
public class Employee {
  @PrimaryKey
  private String user;

  @Persistent
  private Team team;
}

To get the Employee you can query by email:

UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();

PersistenceManager pm = PMF.get().getPersistenceManager();
Employee employee = pm.getObjectById(Employee.class, user.getEmail());
Team employee.getTeam();

If a user can be in many teams, you need to use an unowned relationship. From your description, it sounds like each Team has one User (?), so you might want an unowned many-to-many relationship.

The part that isn't really well documented is that you cannot use a User object as a primary key (the possible types for a primary key are quite limited), so you end up often using the email as the primary key. A User object can be a field, but to make your data model consistent, you often either use the email everywhere to refer to the user, or you use a wrapper object like I did above.

NamshubWriter
i'm getting an error using your method of: Exception converting [email protected] to an internal key.I think it could be coming from somewhere else in my code but having trouble finding the source of it
AphexMunky
Sorry, just seen your edit that you cant use a User as a primary key
AphexMunky
Thank you, solved my problem
AphexMunky