我无法从 Mongo 获取 dbRef 对象。在我的实体包中,我有一个类继承User
了一个类。Parent
这是User
课程:
public class User {
@Id
private ObjectId id;
@DBRef
private Account account;
private String name;
public String getId() {
if (id != null) {
return id.toStringMongod();
}
return null;//no id
}
public void setId(ObjectId objectId) {
this.id = objectId;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
}
正如你在上面看到的,我把一个对象放在Account
这里。我的Parent
课只是扩展User
:
@Document
public class Parent extends User {
@JsonProperty("is_activated")
private boolean isActivated;
public boolean isActivated() {
return isActivated;
}
public void setActivated(boolean isActivated) {
this.isActivated = isActivated;
}
}
注意:没有什么神奇的isActivated
。
在我的ParentDaoImpl
课堂上:
@Service
public class ParentDaoImpl extends AbstractDaoImpl implements ParentDao {
@Override
public Parent getParentByLogin(String login) {
Query query = new Query(Criteria.where("login").is(login));
return mongoOperations.findOne(query, Parent.class, "parents");
}
}
问题是,如果我调用getParentByLogin
方法,它会返回 evertyning 但Account
字段为空。也许findOne
里面没有给 dbRef。我认为在关系数据库中,会有类似join
. 我希望我的方法也能给我带来account
领域。
谢谢你的帮助!