2

I cannot get dbRef object from Mongo. In my entity package I have a User class with a Parent class inheriting. Here is the User class:

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;
}
}

As you can see above, I am putting an object of Account here. My Parent class simply extends 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;
}
}

Note: nothing magic with isActivated.

In my ParentDaoImpl class:

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

The problem is that if I call getParentByLogin method, it returns evertyning but Account field is null. Maybe findOne doesn't give dbRef inside. I think in relational Databases, there would be something like join. I want my method to give me account field as well.

Thanks for your help!

boburShox
  • 2,630
  • 5
  • 23
  • 35
  • 1
    You have actually set anything into the account field? If it returns null I guess it's...null. Not sure if the mongodb java driver automatically resolves the dbref. Maybe you need to add some Annotation (configuration) to autoload dbrefs. – philnate Feb 21 '13 at 08:12
  • I didn't mention `Account` class there. Creating Account before `getParentBylogin` is provided. I mean I first created account for the parent(I can get it in mongo shell or through my app as well), and then tried to get the whole parent – boburShox Feb 21 '13 at 08:22
  • 1
    Do you use the pure mongodb java driver or some framework around it like Spring Data/morphia? @Service looks like Spring Data. Maybe you should give a short example how you populate your database in the first place. Maybe there's the error. – philnate Feb 21 '13 at 08:38
  • I am using Spring. I was actually careful about testing that before I post here. Unfortunately, now I got it worked. I apologize for that! Thanks anyway. – boburShox Feb 21 '13 at 09:42

1 Answers1

2

Can you try something like this.

....
@Field("fieldName")
@DBRef(collection = "mongoCollectionName")
private Account account;
....
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
  • thanks for the response. I tried it, no success. Still returning null, even though there is a specific account related to the parent object – boburShox Feb 21 '13 at 08:30
  • First time I tried with the aid of your response seemed fail. But recently, I made it. Thank you very much! – boburShox Mar 06 '13 at 05:19
  • when i add the @DBRef(collection = "mongoCollectionName") it says theres no method called collection – GeekySelene Apr 29 '19 at 07:31