2013-02-13 72 views
0

我正在使用Java & GAE數據存儲爲我的應用程序存儲,管理和檢索一些數據。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 
    .... 
} 

回答

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(); 
+0

感謝spiritwalker,它幾乎在工作..我可以成功地檢索客戶,但在結果裏面對象沒有商店..我也用客戶和商店類代碼更新了我的問題。實際上,感謝您的幫助 – BeNdErR 2013-02-13 13:36:22

+0

,我錯過了一行代碼,以顯示商店。我的錯!您的解決方案有效,謝謝! – BeNdErR 2013-02-13 15:51:17