У меня есть следующий класс:
@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();
}
Когда я устанавливаю массив parent как 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 ("Прочитайте Foo"). Что происходит, что Spring не может снова восстановить объекты?
Лучший, Педро.
Lazy loading Db-Refs в Spring Data MongoDB поддерживается только в том случае, если тип свойства является не финальным классом или интерфейсом, поскольку нам нужно создать для него JDK или CGLib Proxy. Массивы не поддерживаются. Я создал https://jira.spring.io/browse/DATAMONGO-1157, чтобы отслеживать это.
List<Foo>
мой массив, он должен работать?