• 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 + "]";
        }
    
    }
  • 相关阅读:
    springcloud-04-自定义ribbon的配置方式
    springcloud-03-服务注册
    springcloud-02-eureka
    springcloud-01-介绍
    使用jdk生成ssl证书文件
    CentOS7忘记root密码的修改方法
    2.1 Scala语言概述
    1.大数据技术概述
    KL散度=交叉熵-熵
    7.数据结构---图(遍历)
  • 原文地址:https://www.cnblogs.com/jiangwz/p/8064297.html
Copyright © 2020-2023  润新知