spring mvc报错,数据库查询无限死循环

进行查询的陷入了无限死循环,原因是问题类中包含了回答,回答类中包含了问题,进入了无限死循环

解决方法:在回答类中的问题类属性上加注解:@JsonBackReference

问题中有回答的set集合,回答中有问题类:

问题类:

package com.wazn.learn.entity.teachclass;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.wazn.learn.entity.sys.User;

/**
 * 
 * 提问表
 *
 */
@Entity
@Table(name="teach_questions")
public class Questions {
    private Integer id;//id
    private User userid;//学生id
    private TeachClass cid;//课程id
    private String question;//提问
    private Date twtime;//提问时间
    private String title;
    private Set<Answer> answer;
    
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Column(nullable=false)
    
    public String getQuestion() {
        return question;
    }
    public void setQuestion(String question) {
        this.question = question;
    }
    @ManyToOne
    @JoinColumn(name="c_id",nullable=false,insertable=true)
    public TeachClass getCid() {
        return cid;
    }
    public void setCid(TeachClass cid) {
        this.cid = cid;
    }
    @ManyToOne
    @JoinColumn(name="user_id",nullable=false,insertable=true)
    public User getUserid() {
        return userid;
    }
    public void setUserid(User userid) {
        this.userid = userid;
    }
    public Date getTwtime() {
        return twtime;
    }
    public void setTwtime(Date twtime) {
        this.twtime = twtime;
    }
    
 @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE },mappedBy ="question")
    public Set<Answer> getAnswer() {
        return answer;
    }
    public void setAnswer(Set<Answer> answer) {
        this.answer = answer;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

回答类:

package com.wazn.learn.entity.teachclass;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.wazn.learn.entity.sys.User;
@Entity
@Table(name="teach_answer")
public class Answer {
    
    private Integer id;
    private String content;
    private Date hftime;
    private User userid;//教师id
    private Questions question;
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Column(nullable=false)
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    public Date getHftime() {
        return hftime;
    }
    public void setHftime(Date hftime) {
        this.hftime = hftime;
    }
    @ManyToOne
    @JoinColumn(name="u_id",nullable=false,insertable=true)
    public User getUserid() {
        return userid;
    }
    public void setUserid(User userid) {
        this.userid = userid;
    }
    
    @ManyToOne
    @JoinColumn(name="q_id",nullable=false,insertable=true)
    @JsonBackReference
    public Questions getQuestion() {
        return question;
    }
    @JsonBackReference
    public void setQuestion(Questions question) {
        this.question = question;
    }
    @Override
    public String toString() {
        return "Answer [id=" + id + ", content=" + content + ", hftime=" + hftime + ", userid=" + userid + ", question="
                + question + "]";
    }

}

 

posted @ 2017-12-19 10:37  谁将新樽辞旧月,今月曾经照古人  阅读(731)  评论(0编辑  收藏  举报