DBRef在Spring数据mongodb中延迟加载

时间:2014-04-12 17:50:49

标签: java mongodb spring-data-mongodb

我想在延迟加载时使用@DBRef,但是没有用。我使用Spring数据mongodb(版本1.4.1.RELEASE)。

这是我的样本:

public class Person {

@Id
private String id;
private String firstname;
private String surname;

// Spring create proxy
@DBRef(lazy = true)
private List<Address> addresses;

public boolean hasHomeAddress() {
    if(addresses != null) { // this condition is not required, because "addresses" is proxy
        for(Address address : addresses) { //in this line - NullPoinetrException
            if(AddressType.HOME.equals(address.getType())) {
                return true;
            }
        }
    }
    return false;
}

//getters/setters
}


public class Address {

@Id
private String id;
private String locality;
private String street;
private AddressType type;

public static enum AddressType {
    HOME, OTHER;
}

//getters/setters
public AddressType getType() {
    return type;
}
}

当我运行此代码时:

Person person = personRepository.findOne(personId);
if(person.hasHomeAddress()) {
  //do something
}

比我得到NullPointerExcetpion。执行此代码时抛出异常:

for(Address address : addresses)

我做错了什么?也许这是春天数据mongodb的错误?

0 个答案:

没有答案