当前位置: 首页 > 面试题库 >

无限递归与Jackson JSON和Hibernate JPA问题

逄学潞
2023-03-14
问题内容

当尝试将具有双向关联的JPA对象转换为JSON时,我不断

org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError)

我所发现的只是该线程,基本上以建议避免双向关联为结尾。有谁知道这个春季错误的解决方法?

------编辑2010-07-24 16:26:22 -------

代码段:

业务对象1:

@Entity
@Table(name = "ta_trainee", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Trainee extends BusinessObject {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "name", nullable = true)
    private String name;

    @Column(name = "surname", nullable = true)
    private String surname;

    @OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Column(nullable = true)
    private Set<BodyStat> bodyStats;

    @OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Column(nullable = true)
    private Set<Training> trainings;

    @OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Column(nullable = true)
    private Set<ExerciseType> exerciseTypes;

    public Trainee() {
        super();
    }

    ... getters/setters ...

业务对象2:

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "ta_bodystat", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class BodyStat extends BusinessObject {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "height", nullable = true)
    private Float height;

    @Column(name = "measuretime", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date measureTime;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name="trainee_fk")
    private Trainee trainee;

控制器:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Controller
@RequestMapping(value = "/trainees")
public class TraineesController {

    final Logger logger = LoggerFactory.getLogger(TraineesController.class);

    private Map<Long, Trainee> trainees = new ConcurrentHashMap<Long, Trainee>();

    @Autowired
    private ITraineeDAO traineeDAO;

    /**
     * Return json repres. of all trainees
     */
    @RequestMapping(value = "/getAllTrainees", method = RequestMethod.GET)
    @ResponseBody        
    public Collection getAllTrainees() {
        Collection allTrainees = this.traineeDAO.getAll();

        this.logger.debug("A total of " + allTrainees.size() + "  trainees was read from db");

        return allTrainees;
    }    
}

JPA实施学员DAO:

@Repository
@Transactional
public class TraineeDAO implements ITraineeDAO {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public Trainee save(Trainee trainee) {
        em.persist(trainee);
        return trainee;
    }

    @Transactional(readOnly = true)
    public Collection getAll() {
        return (Collection) em.createQuery("SELECT t FROM Trainee t").getResultList();
    }
}

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
    <persistence-unit name="RDBMS" transaction-type="RESOURCE_LOCAL">
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <!-- <property name="dialect" value="org.hibernate.dialect.HSQLDialect"/>         -->
        </properties>
    </persistence-unit>
</persistence>

问题答案:

您可以使用它@JsonIgnore来打破循环。



 类似资料:
  • 问题内容: 尝试将具有双向关联的JPA对象转换为JSON时,我不断 我所发现的只是该线程,基本上以建议避免双向关联为结尾。有谁知道这个Spring错误的解决方法? 代码段: 业务对象1: 业务对象2: 控制器: JPA实施学员DAO: persistence.xml 问题答案: 你现在可以使用JsonIgnoreProperties到属性(序列化过程中)的抑制序列,或忽略JSON性能的处理读取(反

  • 当尝试将具有双向关联的JPA对象转换为JSON时,我不断得到 我所发现的就是这条线索,它的基本结论是建议避免双向关联。有没有人有办法解决这个spring bug? ------编辑2010-07-24 16:26:22------- 代码片段: 业务对象1: 业务对象2: 控制器: JPA-见习DAO的实施: 坚持不懈xml

  • 我来自Grails背景,最近在Micronaut使用GORM启动了一个项目。 我有以下代码: 应用程序编译和启动没有问题,但当我尝试访问url http:localhost:8080/author时,我收到以下错误: 10:25:29.431[nioEventLoopGroup-1-2]错误i.m.h.s.netty。RoutingInBoundHandler-发生意外错误:将对象[[micron

  • 在尝试执行GET到发布者存储库时,我正在执行GET和无限循环。 出版商: 书: 完整代码可在此处获得: https://github.com/vanyasav/library

  • 我对函数式编程很陌生,尤其是下面使用的Scheme。我正在尝试使以下函数是递归的,尾递归的。基本上,该函数的作用是对两个字符串的对齐方式进行评分。当给定两个字符串作为输入时,它会比较每个“列”字符,并根据在称为 scorer 的函数中实现的评分方案(由下面的代码中的函数调用)来累积该对齐的分数。 我有一个想法,用一个帮助函数来累积分数,但我不太确定如何去做,因此我该如何让下面的函数尾递归呢?

  • 我用的是Spring Roo 1.2.1和Jackson 1.9.7。在使用json序列化我的类时,我得到了一个JsonMappingException。 我读了以下帖子,但没有找到适合我的工作解决方案: Jackson的无限递归 Jackson-具有双方向关系的实体序列化(避免循环) 我不知道为什么JsonIgnore在属性QueueOuts的类Queue中不起作用。我也尝试了JsonManag