遇到需求需返回树状结构,最开始使用递归方式生成树结构,发布后出现栈内存溢出问题;后改为非递归方式
实体类:children节点的get、set方法上加@JsonManagedReference注解,否则会jackson转换报错,也会出现无限递归的情况
package com.sinosoft.ie.sequence.entity;
import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import org.hibernate.annotations.GenericGenerator;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author fy
* @date 2022-03-08
*/
@Entity
@SuppressWarnings("serial")
@Table(name = "SEQUENCETAB")
public class SequenceEntity implements java.io.Serializable {
private String id; // 主键
private String stuName; // 姓名
private Date sequenceDate; // 测序日期
private String contrastZhu; // 对比株
private String cover; // 覆盖率
private String pointPosition; // 点位
private String pid; // 暂留
private String subPoint; // 子位点
private String superiorsPoint; // 父位点
private String sequenceDateString; //
private List<TreeSequenceEntity> children = new ArrayList();
public SequenceEntity() {
}
public SequenceEntity(String id, String stuName, Date sequenceDate, String contrastZhu, String cover, String pointPosition, String pid, String subPoint, String superiorsPoint, String sequenceDateString) {
this.id = id;
this.stuName = stuName;
this.sequenceDate = sequenceDate;
this.contrastZhu = contrastZhu;
this.cover = cover;
this.pointPosition = pointPosition;
this.pid = pid;
this.subPoint = subPoint;
this.superiorsPoint = superiorsPoint;
this.sequenceDateString = sequenceDateString;
}
@Id
@Column(name = "ID")
@GeneratedValue(generator="uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "STU_NAME")
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
@Column(name = "SEQUENCE_DATE")
@JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
public Date getSequenceDate() {
return sequenceDate;
}
public void setSequenceDate(Date sequenceDate) {
this.sequenceDate = sequenceDate;
}
@Column(name = "CONTRAST_ZHU")
public String getContrastZhu() {
return contrastZhu;
}
public void setContrastZhu(String contrastZhu) {
this.contrastZhu = contrastZhu;
}
@Column(name = "COVER")
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
@Column(name = "POINT_POSITION")
public String getPointPosition() {
return pointPosition;
}
public void setPointPosition(String pointPosition) {
this.pointPosition = pointPosition;
}
@Column(name = "PID")
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
@Column(name = "SUB_POINT")
public String getSubPoint() {
return subPoint;
}
public void setSubPoint(String subPoint) {
this.subPoint = subPoint;
}
@Column(name = "SUPERIORS_POINT")
public String getSuperiorsPoint() {
return superiorsPoint;
}
public void setSuperiorsPoint(String superiorsPoint) {
this.superiorsPoint = superiorsPoint;
}
@Transient
public String getSequenceDateString() {
return sequenceDateString;
}
public void setSequenceDateString(String sequenceDateString) {
this.sequenceDateString = sequenceDateString;
}
@Transient
@JsonManagedReference
public List<TreeSequenceEntity> getChildren() {
return children;
}
@JsonManagedReference
public void setChildren(List<TreeSequenceEntity> children) {
this.children = children;
}
}
工具类:
package com.sinosoft.ie.sequence.util;
import com.sinosoft.ie.sequence.entity.SequenceEntity;
import com.sinosoft.ie.sequence.entity.TreeSequenceEntity;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TreeUtils {
public static List<SequenceEntity> buileTreeTest(List<SequenceEntity> list, String pid) {
List<SequenceEntity> rootTrees = new ArrayList<SequenceEntity>();
for (SequenceEntity dicTree : list) {
if (pid.equals(dicTree.getSuperiorsPoint())) {
rootTrees.add(dicTree);
}
for (SequenceEntity childrenDicTree : list) {
TreeSequenceEntity vo = new TreeSequenceEntity();
vo.setId(childrenDicTree.getId());
vo.setStuName(childrenDicTree.getStuName());
vo.setSequenceDate(childrenDicTree.getSequenceDate());
vo.setContrastZhu(childrenDicTree.getContrastZhu());
vo.setCover(childrenDicTree.getCover());
vo.setPointPosition(childrenDicTree.getPointPosition());
vo.setPid(childrenDicTree.getPid());
vo.setSubPoint(childrenDicTree.getSubPoint());
vo.setSuperiorsPoint(childrenDicTree.getSuperiorsPoint());
if (dicTree.getSubPoint().equals(childrenDicTree.getSuperiorsPoint())) {
if (dicTree.getChildren() == null || dicTree.getChildren().size() == 0) {
List<TreeSequenceEntity> myChildrens = new ArrayList<TreeSequenceEntity>();
myChildrens.add(vo);
dicTree.setChildren(myChildrens);
} else {
if (!dicTree.getChildren().contains(vo)) {
dicTree.getChildren().add(vo);
}
}
}
}
}
return rootTrees;
}
}