我想在延迟加载时使用@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的错误?