实体类A里引用了实体类B,并用了DBRef,现在想查询A的部分字段和包含的B里的部分字段出来,发现没办法做到,哪位大神有方法?代码如下:
public class A {
@Id
private int id;
@DBRef
private B b;
}
public class B{
@Id
private int id;
private String name;
}
Dao代码如下,
BasicDBObject queryObject = new BasicDBObject();
queryObject.put("id", id); //A的id
BasicDBObject fieldsObject=new BasicDBObject();
fieldsObject.put("b", 1); //可以拿到B的全部字段
//fieldsObject.put("b.name", 1); //亲测失败,报错是无法给B的id设置成null,未找到原因
Query query=new BasicQuery(queryObject,fieldsObject);
mongoOperation.find(query, A.class);