繁体   English   中英

当MongoDB DBRef lazyness设置为true时,将抛出java.lang.IllegalArgumentException

[英]When MongoDB DBRef lazyness is set to true, java.lang.IllegalArgumentException is throw

我有以下课程:

@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儿子(“读取Foo”)时抛出的。 Spring无法再次重建对象,这是怎么回事?

最好,佩德罗。

仅当属性类型为非最终类或接口时,才支持在Spring Data MongoDB中延迟加载Db-Refs,因为我们需要能够为其创建JDK或CGLib代理。 不支持数组。 我创建了https://jira.spring.io/browse/DATAMONGO-1157进行跟踪。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM