HTTP状态500 - 无法懒惰地初始化角色集合:...,无法初始化代理 - 无会话

时间:2015-08-31 04:58:08

标签: java json spring hibernate

我尝试从spring控制器查询客户列表作为json。当我访问网址时,我可以获得列表没有错误。

-CustomerController

@RequestMapping("getCustomerListRB")
public @ResponseBody List<Customer> getCustomerListsRB() {
    List<Customer> cusList = new ArrayList<Customer>();
    cusList = cusService.getList();
    return cusList;
}

但是当我尝试在客户模型中加入表时 -Customer.java

   @OneToMany(mappedBy = "Customer", cascade = CascadeType.ALL)
    private List<MainOrder> MainOrder;

    public List<MainOrder> getMainOrder() {
        return MainOrder;
    }
    public void setMainOrder(List<MainOrder> mainOrder) {
        MainOrder = mainOrder;
    }

再次运行服务器它有错误如下:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.mdr.model.Customer.mainOrders, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.mdr.model.Customer["mainOrders"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.mdr.model.Customer.mainOrders, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.mdr.model.Customer["mainOrders"])
    org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:256)
    org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:143)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:89)
    org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:193)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

在我添加fetch = FetchType.EAGER后如下

@OneToMany(mappedBy = "Customer", cascade = CascadeType.ALL,fetch = FetchType.EAGER) 

我再次运行服务器它没有错误但无法获取数据。请帮帮我!

3 个答案:

答案 0 :(得分:1)

首先,您需要一个持续时间足以使Session可用于延迟提取相关实体的事务。在您的示例中,您将返回在事务范围之外具有未获取的相关实体的实体列表(Spring internals将在事务结束后将List转换为JSON - 这就是它失败的原因。)

您可以执行以下操作之一:

  1. 在您的关系中略过懒惰,因为无论如何都必须提取所有相关实体以预先显示完整的JSON响应。
  2. 将您的方法修改为:'String getCustomerListsRB()'并使用ObjectMapper自行将'List'序列化为JSON(您可以将ObjectMapper自动装配到您的控制器)。
  3. 尝试使用Open Session In View过滤器,但我强烈建议您不要使用此方法,因为它会影响性能,因为数据库连接的持续时间超过了必要的时间。

答案 1 :(得分:1)

当使用带弹簧的杰克逊时我得到了同样的例外。将@jsonignore添加到声明为lazy的字段或在实体类中创建循环依赖。确保在应用程序的所有层中使用相同的库(codehaus / jackson / ..)jsonignore。

答案 2 :(得分:0)

看看: Spring MVC :could not initialize proxy - no Session (through reference chain:

作为解决方案,您可以使用附加实体注释来阻止延迟初始化

@Entity @Table(name="Country") **@Proxy(lazy = false)** public class Country {}

或在映射中使用@JsonBackReference