问题
I have a retailer class with nested dbref of Address. I would like to fetch retailers based on city which is part of Address class. But I am getting below error.
org.springframework.data.mapping.model.MappingException: Invalid path reference address.city! Associations can only be pointed to directly or via their id property!
Could you please let me know what's wrong and how to fix this?
Code is given below
@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;
}
I am using following query to get the data
@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;
}
Data
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"))
}
来源:https://stackoverflow.com/questions/32416656/spring-mongodb-fetch-data-using-dbref-association