gpt4 book ai didi

java - 通过将 Spring MVC 与 AJAX/JSON 和 MappingJackson 结合使用来引发 LazyInitializationException

转载 作者:行者123 更新时间:2023-11-29 09:46:19 27 4
gpt4 key购买 nike

我正在使用 Spring MVC、AJAX/JSON 和 Hibernate 从 MySQL 数据库中获取所有人。我编写了 JUnit 集成测试来验证我的服务,一切正常。

现在我这样调用它:

@RequestMapping(value="/allpersons", method=RequestMethod.GET)
public @ResponseBody Set<Person> getAllPersons() {
Set<Person> persons= new PersonServiceImpl().getAllPersons();
return persons;
}

我调试过了。行与

return persons;

一切都很好。我有一个包含所有人的 HashSet。调试更多步骤,我来到这一行:

this.objectMapper.writeValue(jsonGenerator, o);

org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

然后我没有看到源代码,但我的调试器告诉我:

StdSerializerProvider._serializeValue(JsonGenerator, Object) line 297

在这一行之后,我得到了错误:

ERROR: org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.mydomain.project.dom.Person.projects, no session or session was closedorg.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mydomain.project.dom.Person.projects, no session or session was closed    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)    at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)    at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:339)    at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:314)    at org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:112)    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:268)    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:160)    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:131)    at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:363)    at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:314)    at org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:112)    at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:297)    at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:224)    at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1030)    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:153)    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:181)    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.writeWithMessageConverters(AnnotationMethodHandlerAdapter.java:975)    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.handleResponseBody(AnnotationMethodHandlerAdapter.java:933)    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:882)    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:428)    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)    at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)    at java.lang.Thread.run(Thread.java:619)

看来,这与序列化/JacksonMapping有关。它想要获取一个人的相关项目,这些项目是不需要的,通常是延迟加载的。

这里有什么问题?

提前致谢并致以最诚挚的问候。

最佳答案

你遇到的情况与此类似: Lazy Loading error

简而言之,您正在访问 Hibernate Session 之外的延迟加载集合。

可能的解决方案:

  • 赶紧拿过来
  • 确保您的 Hibernate Session 在您访问集合时仍然处于打开状态。您可以使用 OpenSessionInView 过滤器。这本身可能还不够,因为 Hibernate 可能会在您关闭事务的地方结束您的 session 。因此,您需要扩展事务范围或找到一种方法来防止 Hibernate 在事务结束时关闭 session 。
  • 确保在事务中访问延迟加载的集合,以确保 Hibernate 加载它。

请查看上面指定的帖子以获取更多详细信息。

更新 1据我了解,这是由 Jackson 映射器接收到一个未完全加载的模型(一些集合是延迟加载的)引起的。你是说你不需要 jackson 映射这些集合中的对象。我看到的替代方案是:

  • 为 Jackson 提供一个正确的模型,即基于您的 Person 对象创建的简单 POJO,仅包含您希望与 Jackson 映射的信息。因此,不是给 Jackson 一个 Persons 列表,而是给它一个 PersonDetails 列表(或者你想怎么调用它们)
  • 将 Jackson 配置为忽略该集合 - 不确定这是否可行

希望这对您有所帮助。

关于java - 通过将 Spring MVC 与 AJAX/JSON 和 MappingJackson 结合使用来引发 LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4377829/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com