GAE数据存储区 - 嵌套类的查询过滤器

时间:2013-02-13 11:11:43

标签: java google-app-engine google-cloud-datastore

我正在使用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
    ....
}

1 个答案:

答案 0 :(得分:2)

查看以下代码是否符合您的要求。

Query query = pm.newQuery(Customer.class);
query.declareVariables("Customer store");
query.setFilter(
    "this.stores.contains(store) && store.city == \"SomeCity\"");
Collection result = (Collection)query.execute();