当MongoDB DBRef lazyness设置为true时,将抛出java.lang.IllegalArgumentException
我有以下课程:当MongoDB DBRef lazyness设置为true时,将抛出java.lang.IllegalArgumentException,java,spring,mongodb,spring-data-mongodb,Java,Spring,Mongodb,Spring Data Mongodb,我有以下课程: @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
@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();
}
当我将父数组设置为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-son(“Read-Foo”)时抛出的。发生了什么,Spring无法重新构建对象
最好的,
Pedro.只有当属性类型为非最终类或接口时,才支持在Spring Data MongoDB中延迟加载Db Refs,因为我们需要能够为其创建JDK或CGLib代理。不支持数组。我创建数据库就是为了跟踪它。只有当属性的类型是非最终类或接口时,才支持在Spring Data MongoDB中延迟加载Db Refs,因为我们需要能够为它创建JDK或CGLib代理。不支持数组。我创建了跟踪功能。感谢您的反馈和错误。我现在无法测试,但是如果我替换了
列表
我的数组,它应该可以工作吗?是的,使用集合类/接口应该可以工作。我确实可以。谢谢你的帮助!感谢您的反馈和错误。我现在无法测试,但是如果我替换了列表
我的数组,它应该可以工作吗?是的,使用集合类/接口应该可以工作。我确实可以。谢谢你的帮助!