当前位置: 首页 > 知识库问答 >
问题:

JAVAlang.stackOverflowerr与jackson json、spring和hibernate[重复]

赫连法
2023-03-14

我有两个双向的一对多关系,我得到了这个stackoverflow例外,但现在不知道在哪里,为什么?每个事件包含一个位置和多个会话。

@Entity
@Table(name = "session")
public class Session {
    @Id
    @Column(name = "idsession")
    int idsession;


 @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "idevent")
    Event event ;

.
.
.
}

事件JAVA

@Entity
    @Table(name="event")
    public class Event {

        @Column(name = "idevent")
        @Id
        private int idevent;

      @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "idevent")
        private List<Session> sessions = new ArrayList<Session>();
               }

地方JAVA

@Entity
@Table(name="location")
public class Location {

    @Id
    @Column(name = "idlocation" )

    @OneToOne
    @JoinColumn(name = "idevent")
    private Event event;
}

控制器

@RequestMapping(value="/events", method=RequestMethod.GET)
public @ResponseBody
List<Event> events() {


    return  eventService.getAllEvents() ;
}

编辑:

EventDaoImpl that eventService uses it. 
    @Repository
    @Transactional(readOnly = true)
    public class EventDaoImpl implements  EventDao {

        @Autowired
        private SessionFactory sessionFactory;

        @Override
        public List<Event> getAllEvents() {
            Session session = sessionFactory.openSession();
            String hql = "FROM Event";
            Query query = session.createQuery(hql);
            List<Event> eventList = query.list();
            System.err.print("lengthhh" + eventList.size());
            return eventList;
        }
}

异常跟踪:

 Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exception
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:501)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.sendServerError(DefaultHandlerExceptionResolver.java:313)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:370)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:140)
    at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:138)
    at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1167)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1004)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
]]

[2016-10-11T22:40:22.158+0330] [glassfish 4.1] [WARNING] [] [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] [tid: _ThreadID=27 _ThreadName=http-listener-1(1)] [timeMillis: 1476213022158] [levelValue: 900] [[
  Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exception
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:501)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.sendServerError(DefaultHandlerExceptionResolver.java:313)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:370)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:140)
    at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:138)
    at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1167)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1004)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
]]

[2016-10-11T22:40:22.162+0330] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=27 _ThreadName=http-listener-1(1)] [timeMillis: 1476213022162] [levelValue: 900] [[
  StandardWrapperValve[mvc-dispatcher]: Servlet.service() for servlet mvc-dispatcher threw exception
java.lang.StackOverflowError
    at java.lang.Exception.<init>(Exception.java:66)
    at java.io.IOException.<init>(IOException.java:58)
    at com.fasterxml.jackson.core.JsonProcessingException.<init>(JsonProcessingException.java:27)
    at com.fasterxml.jackson.core.JsonProcessingException.<init>(JsonProcessingException.java:46)
    at com.fasterxml.jackson.databind.JsonMappingException.<init>(JsonMappingException.java:149)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:658)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)

问题解决了:

为两个具有双向关系的字段添加“JsonIgnore”后,问题得到了解决@onetoOne和@OneToMany

共有2个答案

桓信鸥
2023-03-14

您的错误(您的堆栈跟踪证实了这一点)是杰克逊序列化试图解析您的Event会话类之间的引用圈的结果。大多数序列化框架需要您的一些提示来解决这些循环引用,否则它们最终会:

  • 序列化事件"A"
  • 序列化事件"A"的所有会话
  • 序列化第一个会话的父级,即事件"A"
  • 再次序列化事件"A"...

您应该做什么通过指示序列化程序忽略父对象来打破这个循环,为此您可以使用:

@JsonIgnore

处理相同主题的特定stackoverflow问题:
Json和Java-循环参考

喻昀
2023-03-14

我假设在json序列化期间有一个循环引用。因此,如果您开始将会话对象序列化为包含事件字段的json,jackson也会尝试序列化引用的事件对象。问题是事件对象本身引用了会话对象。因此,如果jackson到达事件对象中的会话字段,它会尝试再次序列化会话对象。这将最终进入无限循环并导致堆栈溢出错误。

因此,您需要使用@JsonIgnore注释或类似的方法从序列化过程中排除这两个字段中的一个(会话或事件)。从哪个类中排除哪个字段取决于应用程序的需要,因为它会影响生成的json的结构。

 类似资料:
  • 公共类用户实现Serializable{ 而metghod是 }

  • 我在我的项目中使用Spring 4.0.0 RELEASE jars, hibernate-coe 4.3.0 jar。我面临着。实际上在旧的hibernate核心jars中,我们有这个类,但在4.3.0版本中没有。事实上,我们在4.3.0版本中有。但我不确定这个类在运行时是如何依赖的。意思是当我使用glassfish服务器到我的应用程序时。 请让我知道Spring 4.0.0和hibernate

  • 问题内容: 如果有什么问题请指正。 现在,当我们将Spring DAO用于ORM模板时,当我们使用@Transactional属性时,从外部而不是在方法内部调用该方法时,我们将无法控制事务和/或会话。 延迟加载可节省资源-减少对数据库的查询,减少将所有集合保持在应用程序内存中的内存。 因此,如果lazy = false,则在链接集中有10,000条记录的情况下,所有相关联的集合都将被提取,这并不是

  • 问题内容: 我的项目使用spring3 jpa(hibernate),但是我在使用Hibernate Persistence类时遇到了问题……所以我无法正确创建我的entityManager,并且问题不断发生…… 我相信问题在我的pom.xml文件中!(如果这不是问题,那么也许在我的应用程序COntext.xml中?不这么认为,但他知道…) 这是pom.xml 现在错误输出: 您可以看到它是一个嵌

  • 我试图向现有的Spring应用程序添加两个额外的表。一个正在数据库中创建,但另一个没有。我看不到JPA对象有任何明显的区别,我已经更新了这两个对象的DB属性。两个表之间唯一的主要区别是,一个表以双向关系映射到用户的表,另一个表只有一个方向。 以下是一些代码示例: 持久性.xml: data-context.xmlJPA声明: 产品实体: 支付实体: 用户实体: 账户实体: 产品表已创建 ,并链接到

  • 问题内容: 在哪些典型的现实生活场景中,人们会选择Spring Data JDBC / Spring Data JPA与Hibernate?我想了解最适合这两种实现方式的场景。 问题答案: 正如@Naros所说,标题中当前存在的问题实际上并没有解决。似乎我们应该真正看一下4个选项,并且主要列出每种方法的优点,缺点是没有其他方法的优点: 没有Spring数据的JDBC 您可以对所发生的事情进行100