[英]GAE Datastore - query filter on nested class
我正在使用Java和GAE数据存储来存储,管理和检索我的应用程序的一些数据。
基本上我有两个类:Customer和Store。 这个想法是,一个客户可以关联多个商店,并且可以存在许多客户。
客户的结构是这样的:
<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>
正如我之前所说,可能有很多客户:
<Customers>
<Customer>
...
</Customer>
<Customer>
...
</Customer>
</Customers>
我需要构建一个过滤器来获取这些客户的一部分,将查询传递给“CITY”参数:例如,如果我想要显示至少有一个位于威尼斯的商店的客户,我会做类似的事情(只是为了给你这个想法)
GET Customer WHERE Customer.Store.City = 'Venice'
我想得到的是每个客户都有一个位于特定城市的商店..而且,客户对象需要拥有这些商店! 像这样:
<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>
我可以正确地获得这些商店,但我需要找到一种方法将每家商店连接到他的祖先客户。
String city = 'something';
Query query = mgr.newQuery(Store.class, "city == '"+city+"' ");
List<Store> oggetti = (List<Store>) query.execute();
有关如何做到这一点的任何想法?
我希望我足够清楚......先谢谢,最诚挚的问候
附加信息:
班级客户:
@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
....
}
类商店:
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@Cacheable("false")
public class Store {
@PrimaryKey
@Persistent
private String storeUniqueKey = "";
@Persistent
private String city = "";
//getters and setters here
....
}
看看以下代码是否符合您的要求。
Query query = pm.newQuery(Customer.class);
query.declareVariables("Customer store");
query.setFilter(
"this.stores.contains(store) && store.city == \"SomeCity\"");
Collection result = (Collection)query.execute();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.