Spring Mongodb fetch data using DBRef association

不问归期 提交于 2019-12-12 14:12:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!