DBRef(lazy = true)引用上的空对象

时间:2018-10-03 09:20:45

标签: spring-data-mongodb

关于我的问题的简单测试案例的新帖子

当访问属性时,类的惰性DBRef应该触发LazyLoadingInterceptor并为我提供引用背后的真实对象。


测试失败

public class TestLazyLoadNotWorking {

    @Autowired
    ARepository aRepository;

    @Before
    public void setUp(){
        aRepository.deleteAll();
    }

    @Test
    public void TestLazyLoadNotWorking(){
        A a1 = new A("a1");
        A a2 = new A("a2");
        a1.a = a2;
        aRepository.save(a1);
        aRepository.save(a2);
        aRepository.findById("1").ifPresent(a -> assertThat(a.a.a_property).isNotNull());
    }

}

数据类

@Document
public class A {

    private static int counter = 0;

    @Id
    String id = Integer.toString(++counter);

    @DBRef(lazy = true)
    public A a;

    public String a_property;

    public A(String a_property){ this.a_property = a_property;}
}

0 个答案:

没有答案