Reputation: 17929
I'm using Java & GAE datastore to store, manage and retrieve some data for my application.
Basically I have two classes: Customer and Store. The idea is that one customer can have more than one store associated, and many customers can exist.
The structure of a customer is something like this:
<Customer>
<id>id1</id>
<Stores>
<Store>
<storeId>100</storeId>
<city>Venice</city>
</Store>
<Store>
<storeId>200</storeId>
<city>Milan</city>
</Store>
<Store>
<storeId>300</storeId>
<city>Venice</city>
</Store>
</Stores>
</Customer>
As I said before, there can be MANY customers:
<Customers>
<Customer>
...
</Customer>
<Customer>
...
</Customer>
</Customers>
I need to build a filter to get only a portion of these customers, passing to a query a "CITY" parameter: For example, if I want to show Customers that has at least one Store located in Venice, I would do something like ( just to give you the idea)
GET Customer WHERE Customer.Store.City = 'Venice'
What I'd like to get is every Customer that has a Store located in a particular city.. but also, the Customer Object needs to have those stores! like this:
<Customer>
<id>id1</id>
<Stores>
<Store>
<storeId>100</storeId>
<city>Venice</city>
</Store>
<Store>
<storeId>300</storeId>
<city>Venice</city>
</Store>
</Stores>
</Customer>
<Customer>
<id>id2</id>
<Stores>
<Store>
<storeId>700</storeId>
<city>Venice</city>
</Store>
</Stores>
</Customer>
I can get those stores correctly, but I need to find a way to connect every store to his ancestor customer..
String city = 'something';
Query query = mgr.newQuery(Store.class, "city == '"+city+"' ");
List<Store> oggetti = (List<Store>) query.execute();
Any idea on how to do this?
I hope I was clear enough.. thanks in advance, best regards
Additional info:
class Customer:
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@Cacheable("false")
@FetchGroup(name="customerstores", members={@Persistent(name="storeList")})
public class Customer {
@PrimaryKey
@Persistent
private String id= "";
@Persistent
@Unowned
private List<Store> storeList;
//getters and setters here
....
}
Class Store:
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@Cacheable("false")
public class Store {
@PrimaryKey
@Persistent
private String storeUniqueKey = "";
@Persistent
private String city = "";
//getters and setters here
....
}
Upvotes: 0
Views: 265
Reputation: 2257
See if following code could fit your requirement.
Query query = pm.newQuery(Customer.class);
query.declareVariables("Customer store");
query.setFilter(
"this.stores.contains(store) && store.city == \"SomeCity\"");
Collection result = (Collection)query.execute();
Upvotes: 2