我有以下课程:
@Document(collection = "T_FOO")
public class Foo implements Serializable {
@Field
private String name;
@Field
private String observations;
@DBRef
@Field
private Foo[] parents;
}
在此测试中取得了成功:
@Test
public void testFooWithParents() throws Exception {
//mock User
User user = new User(); user.setLogin("admin");
when(userService.getUserWithAuthorities()).thenReturn(user);
// Create Father
restFooMockMvc.perform(post("/app/rest/foos")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJson(fooFather)))
.andExpect(status().isOk());
// Create Mother
restFooMockMvc.perform(post("/app/rest/foos")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJson(fooMother)))
.andExpect(status().isOk());
foo.setParents(new Foo[]{fooFather, fooMother});
// Create Foo
restFooMockMvc.perform(post("/app/rest/foos")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJson(foo)))
.andExpect(status().isOk());
// Read Foo
MvcResult result = restFooMockMvc.perform(get("/app/rest/foos/{id}", DEFAULT_ID))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(DEFAULT_ID))
.andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString()))
.andExpect(jsonPath("$.observations").value(DEFAULT_OBSERVATIONS))
.andExpect(jsonPath("$.parents[0].id").value(FATHER_ID))
.andExpect(jsonPath("$.parents[1].id").value(MOTHER_ID))
.andReturn();
String content = result.getResponse().getContentAsString();
}
当我将parents数组设置为lazy = true
时,我遇到了这个异常:
org.springframework.web.util.NestedServletException:
Request processing failed; nested exception is java.lang.IllegalArgumentException:
Cannot subclass final class class [Lorg.domain.Foo;
这是在我请求Foo儿子(“Read Foo”)的那一刻抛出的。发生了什么,Spring无法再次重新构建对象?
最佳, 佩德罗。
答案 0 :(得分:4)
在Spring数据中延迟加载Db-Refs只有当属性的类型是非最终类或接口时才支持MongoDB,因为我们需要能够为它创建JDK或CGLib代理。不支持数组。我创建了https://jira.spring.io/browse/DATAMONGO-1157来跟踪此事。