Thursday, October 1, 2009

How to mimic SQL WHERE clause on Google Datastore queries

This puzzled me for a while as I never used JDO before crashing the app-engine party.

Assuming we have an Order class with a member variable named customer of type User:

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

//prepare query
PersistenceManager pm = PMF.get().getPersistenceManager();
String select_query = "select from " + Order.class.getName();
Query query = pm.newQuery(select_query);
query.setFilter("customer == paramCustomer");
query.declareParameters("com.google.appengine.api.users.User paramCustomer");

//execute query
List<OrderDTO> result= OrderUtils.getOrderDTOList((List<Order>) query.execute(user));

//feck-off --> I'll never get an MVP but I can swear on my blog!
pm.close();

Just wanted to share to spare some random googler (you) the same process.

No comments: