Java GAE数据存储-嵌套类上的查询筛选器
我正在使用Java&GAE数据存储来存储、管理和检索我的应用程序的一些数据 基本上我有两类:顾客和商店。这个想法是,一个客户可以有多个相关的商店,许多客户可以存在 客户的结构如下所示:Java GAE数据存储-嵌套类上的查询筛选器,java,google-app-engine,google-cloud-datastore,Java,Google App Engine,Google Cloud Datastore,我正在使用Java&GAE数据存储来存储、管理和检索我的应用程序的一些数据 基本上我有两类:顾客和商店。这个想法是,一个客户可以有多个相关的商店,许多客户可以存在 客户的结构如下所示: <Customer> <id>id1</id> <Stores> <Store> <storeId>100</storeId>
<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>
<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>
我想得到的是每一位在特定城市有商店的顾客。。但是,客户对象也需要有这些商店!像这样:
<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>
<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>
看看下面的代码是否符合您的要求
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,它几乎起作用了。。我可以成功地检索客户,但结果对象中没有商店。。我还用客户和商店类代码更新了我的问题。谢谢你的帮助事实上,我遗漏了一行代码,用来显示商店。我的错!您的解决方案有效,谢谢!
@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();