我有一个带有嵌套 dbref 地址的零售商类。我想根据属于 Address 类的城市来获取零售商。但我得到了错误。
org.springframework.data.mapping.model.MappingException:路径引用地址无效。城市!只能直接或通过其 id 属性指向关联!
你能否让我知道出了什么问题以及如何解决这个问题?
代码如下
@Document(collection="retailer")
@TypeAlias("retailer")
public class Retailer extends CommonDomainAttributes implements Serializable {
public Retailer() {
// TODO Auto-generated constructor stub
}
@DBRef
private List<Product> products;
private String shopName;
@DBRef
private Address address;
}
@Document(collection="address")
@TypeAlias("address")
public class Address extends CommonDomainAttributes implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8820483439856446454L;
private String addrLine1;
private String addrLine2;
private String locality;
private String city;
private String state;
private String country;
}
我正在使用以下查询来获取数据
@Override
public List<Retailer> findRetailersByProductNameAndCity(String productName,
String city) {
Query query = Query.query(Criteria.where/*("product.productName").is(productName).and*/("address.city").is(city));
//BasicQuery basicQuery = new BasicQuery("{ product.productName : { $eq : '"+productName+"' }, address.city : { $eq : '"+city+"' }}");
//System.out.println(basicQuery.toString());
//query.fields().include("user");
//query.fields().include("address");
List<Retailer> retailers = mongoTemplate.find(query, Retailer.class);
return retailers;
}
数据
db.retailer.find().pretty();
{
"_id" : ObjectId("55eb14e077c8f563fb2c11ab"),
"_class" : "retailer",
"createDate" : ISODate("2015-09-05T16:14:24.489Z"),
"lastModifiedDate" : ISODate("2015-09-05T16:14:24.489Z"),
"createdBy" : "UnAuntenticatedUser",
"lastModifiedBy" : "UnAuntenticatedUser",
"user" : DBRef("IdeaRealtyUser", ObjectId("55eb14e077c8f563fb2c11aa")),
"products" : [
DBRef("product", ObjectId("55eb14e077c8f563fb2c1193")),
DBRef("product", ObjectId("55eb14e077c8f563fb2c1194")),
DBRef("product", ObjectId("55eb14e077c8f563fb2c119a"))
],
"address" : DBRef("address", ObjectId("55eb14e0a1fd2e78e05053c2"))
}
{
"_id" : ObjectId("55eb14e077c8f563fb2c11ad"),
"_class" : "retailer",
"createDate" : ISODate("2015-09-05T16:14:24.561Z"),
"lastModifiedDate" : ISODate("2015-09-05T16:14:24.561Z"),
"createdBy" : "UnAuntenticatedUser",
"lastModifiedBy" : "UnAuntenticatedUser",
"user" : DBRef("IdeaRealtyUser", ObjectId("55eb14e077c8f563fb2c11ac")),
"products" : [
DBRef("product", ObjectId("55eb14e077c8f563fb2c1193")),
DBRef("product", ObjectId("55eb14e077c8f563fb2c1194")),
DBRef("product", ObjectId("55eb14e077c8f563fb2c119b")),
DBRef("product", ObjectId("55eb14e077c8f563fb2c119f")),
DBRef("product", ObjectId("55eb14e077c8f563fb2c11a0"))
],
"address" : DBRef("address", ObjectId("55eb14e0a1fd2e78e05053c1"))
}