【问题标题】:GAE Datastore - query filter on nested classGAE Datastore - 嵌套类的查询过滤器
【发布时间】:2013-01-28 22:02:16
【问题描述】:

我正在使用 Java 和 GAE 数据存储来存储、管理和检索我的应用程序的一些数据。

基本上我有两个类:客户和商店。这个想法是一个客户可以关联多个商店,并且可以存在许多客户。

客户的结构是这样的:

<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
    ....
}

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢spiritwalker,它几乎可以工作了。我可以成功检索客户,但结果对象内部没有商店。我还用客户和商店类代码更新了我的问题。感谢您的帮助
    • 实际上,我缺少一行代码来显示商店。我的错!您的解决方案有效,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2014-06-16
    相关资源
    最近更新 更多