Spring Mongodb使用DBRef关联获取数据

时间:2015-09-05 18:58:00

标签: java spring mongodb spring-mongodb

我有一个零售商类,其嵌套的地址为dbref。我想根据城市获取零售商,这是Address类的一部分。但我收到了以下错误。

  

org.springframework.data.mapping.model.MappingException:路径无效   参考地址。关联只能直接指向   或通过他们的身份财产!

你能告诉我什么是错的以及如何解决这个问题吗?

代码如下:

@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"))
}

0 个答案:

没有答案