2

我无法从 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领域。

谢谢你的帮助!

4

1 回答 1

2

Can you try something like this.

....
@Field("fieldName")
@DBRef(collection = "mongoCollectionName")
private Account account;
....
于 2013-02-21T08:12:37.327 回答