前言:
在一次项目中,分页查询公告列表信息后,在遍历查询到的公告列表时出现了死循环“There is a cycle in the hierarchy”错误,分析原因是因为在公告实体类中包含了商铺对象导致,所以在遍历的时候需要过滤掉商铺这个字段。
代码示例:
公告实体类
/** * * 电商-公告 * EshopNotice entity.*/ @Entity @Table(name = "eshop_notice") @JsonIgnoreProperties(value={"shop"}) public class EshopNotice implements java.io.Serializable { // Fields // 系统ID private String sysid; //时间戳记 private String tstamp; // 操作日期 private String operationDateTime; // 操作员 private String operator; /** * 商铺 */ private CoreCompany shop; /** * 标题 */ private String title; /** * 内容 */ private String content; /** * 发布日期 */ private String publishDatetime; /** * 状态 */ private String status; // Constructors /** default constructor */ public EshopNotice() { } /** minimal constructor */ public EshopNotice(String tstamp, String operationDateTime, String operator, String title, String content, String publishDatetime, String status) { this.tstamp = tstamp; this.operationDateTime = operationDateTime; this.operator = operator; this.title = title; this.content = content; this.publishDatetime = publishDatetime; this.status = status; } /** full constructor */ public EshopNotice(String tstamp, String operationDateTime, String operator, CoreCompany shop, String title, String content, String publishDatetime, String status) { this.tstamp = tstamp; this.operationDateTime = operationDateTime; this.operator = operator; this.shop = shop; this.title = title; this.content = content; this.publishDatetime = publishDatetime; this.status = status; } // Property accessors @GenericGenerator(name = "generator", strategy = "uuid.hex") @Id @GeneratedValue(generator = "generator") @Column(name = "sysid", unique = true, nullable = false, length = 32) public String getSysid() { return sysid; } public void setSysid(String sysid) { this.sysid = sysid; } @Column(name = "tstamp", nullable = false, length = 20) public String getTstamp() { return tstamp; } public void setTstamp(String tstamp) { this.tstamp = tstamp; } @Column(name = "operationdatetime", nullable = false, length = 20) public String getOperationDateTime() { return operationDateTime; } public void setOperationDateTime(String operationDateTime) { this.operationDateTime = operationDateTime; } @Column(name = "operator", nullable = false, length = 32) public String getOperator() { return this.operator; } public void setOperator(String operator) { this.operator = operator; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "shop", nullable = false) public CoreCompany getShop() { return this.shop; } public void setShop(CoreCompany shop) { this.shop = shop; } @Column(name = "title", nullable = false, length = 128) public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } @Column(name = "content", nullable = false, length = 2000) public String getContent() { return this.content; } public void setContent(String content) { this.content = content; } @Column(name = "publishdatetime", nullable = false, length = 20) public String getPublishDatetime() { return publishDatetime; } public void setPublishDatetime(String publishDatetime) { this.publishDatetime = publishDatetime; } @Column(name = "status", nullable = false, length = 32) public String getStatus() { return this.status; } public void setStatus(String status) { this.status = status; }
分页查询遍历
@RequestMapping("/listPage.html") public JSONTableDateView noticeList(HttpServletRequest request,PageQuery pageQuery) { CoreMember member=(CoreMember)request.getSession().getAttribute("member"); CoreCompany company=coreCompanyService.getByMemberId(member.getSysid()); //分页查询 PageResults<EshopNotice> pageResults = noticeService.noticeList(pageQuery,company.getSysid()); //设置页面参数 JSONArray data = new JSONArray(); for(EshopNotice eshopNotice : pageResults.getResults()){ JsonConfig jsonConfig=new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { @Override public boolean apply(Object arg0, String arg1, Object arg2) { //过滤段公告中的shop字段,否则会无限死循环 if (arg1.equals("shop") ) { return true; } else { return false; } } }); JSONObject dataTemp =JSONObject.fromObject(eshopNotice,jsonConfig); dataTemp.put("title", eshopNotice.getTitle()); dataTemp.put("content", eshopNotice.getContent()); if(eshopNotice.getStatus().equals("00")){ dataTemp.put("status","申请"); }else{ dataTemp.put("status","审核通过"); } dataTemp.put("publishDatetime",eshopNotice.getPublishDatetime()); dataTemp.put("sysid", eshopNotice.getSysid()); data.add(dataTemp); } JSONTableDateView jSONTableDateView = new JSONTableDateView(pageQuery, pageResults, data); return jSONTableDateView; }