Question

I am playing with google app engine and having a bit of trouble on JDOQL queries.

The example shows how to fetch stuff from the datastore:

PersistenceManager pm = PMF.get().getPersistenceManager();
String query = "select from " + Greeting.class.getName();
List<Greeting> greetings = (List<Greeting>) pm.newQuery(query).execute();

But what if I want to fetch stuff only for a given user?

At the bottom of the page in the link above it shows we could something like:

select from guestbook.Greeting where author == 'alfred@example.com'

But if I try the following it doesn't fetch anything:

PersistenceManager pm = PMF.get().getPersistenceManager();
String query = "select from " + Greeting.class.getName() + " where author == '" + user.GetEmail() + "'";
List<Greeting> greetings = (List<Greeting>) pm.newQuery(query).execute();

where user is fetched as shown in the same example like this:

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

i am sure this is a common task but I just can't get it to work - any help appreciated!

Was it helpful?

Solution

After a discussion on this thread on Google Groups I got it working like this:

String select_query = "select from " + Greeting.class.getName(); 
Query query = pm.newQuery(select_query); 
query.setFilter("author == paramAuthor"); 
query.declareParameters("java.lang.String paramAuthor"); 
greetings = (List<Greeting>) query.execute(user);

JDQL syntax is puzzling ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top