標籤:des style java io for ar 2014 art 問題
原因分析在解析bean時,出現死迴圈調用,即多個bean之間出現了相互調用.解決方案:將關聯關係中實體物件間
的lazy屬性設為false過濾掉bean中引起死迴圈調用的屬性。(兩種過濾方式)
//採用數組的方式過濾關聯的實體物件
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.setExcludes(new String[] { "articleComment", "article",
"commentator" });
//重寫apply方法的過濾關聯對象
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
@Override
public boolean apply(Object source, String name, Object object) {
if (name.equals("article") || name.equals("articleComment")
|| name.equals("commentator")) {
return true;
}
return false;
}
});
上面兩種方式雖然可以解決問題但是我沒卻沒法獲得關聯實體物件的屬性因為這些關聯的實體物件都被過濾了。
自訂bean(如果你想要關聯對象的屬性時)
AtricleCommentUitl.java
package com.smsv.app.util;
import java.util.Date;
public class ArticleCommentUtil {
private Long id;
private Date commentTime;
private String content;
private String commentator;
private String replyer;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCommentTime() {
return commentTime;
}
public void setCommentTime(Date commentTime) {
this.commentTime = commentTime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getCommentator() {
return commentator;
}
public void setCommentator(String commentator) {
this.commentator = commentator;
}
public String getReplyer() {
return replyer;
}
public void setReplyer(String replyer) {
this.replyer = replyer;
}
}
article.java
package com.smsv.app.model;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
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.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Copyright ? 2014 All Rights Reserved.
*/
@Entity
@Table(name = "articles")
public class Article implements Serializable {
private Long id;
private String pathCategory;
private String contentCategory;
private String title;
private String body;
private String tags;
private Date creationTime;
private User creator;
private CareerPath careerPath;
private Set<CollectionArticle> collectionArticles = new HashSet<CollectionArticle>();
private Set<ArticleComment> articleComments = new HashSet<ArticleComment>();
private boolean deleted;
public Article() {
// TODO Auto-generated constructor stub
}
public Article(String pathCategory, String tags) {
this.pathCategory = pathCategory;
this.tags = tags;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPathCategory() {
return pathCategory;
}
public void setPathCategory(String pathCategory) {
this.pathCategory = pathCategory;
}
public String getContentCategory() {
return contentCategory;
}
public void setContentCategory(String contentCategory) {
this.contentCategory = contentCategory;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(length = 1000)
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public Date getCreationTime() {
return creationTime;
}
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "creator_id")
public User getCreator() {
return creator;
}
public void setCreator(User creator) {
this.creator = creator;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "article", cascade = { CascadeType.REMOVE })
public Set<CollectionArticle> getCollectionArticles() {
return collectionArticles;
}
public void setCollectionArticles(Set<CollectionArticle> collectionArticles) {
this.collectionArticles = collectionArticles;
}
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.REMOVE }, mappedBy = "article")
public Set<ArticleComment> getArticleComments() {
return articleComments;
}
public void setArticleComments(Set<ArticleComment> articleComments) {
this.articleComments = articleComments;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "path_id")
public CareerPath getCareerPath() {
return careerPath;
}
public void setCareerPath(CareerPath careerPath) {
this.careerPath = careerPath;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Article)) {
return false;
}
final Article article = (Article) o;
return !(id != null ? !article.equals(article.id) : article.id != null);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return (id != null ? id.hashCode() : 0);
}
}
下面是為自訂的bean賦值的過程。
List<ArticleCommentUtil> jsonList = new ArrayList<ArticleCommentUtil>();
List<ArticleComment> articleComments = articleCommentManager
.getArticleComments(articleId);
ArticleCommentUtil articleCommentUtil;
for (ArticleComment articleComment : articleComments) {
articleCommentUtil = new ArticleCommentUtil();
articleCommentUtil.setId(articleComment.getId());
articleCommentUtil.setContent(articleComment.getContent());
articleCommentUtil.setCommentTime(articleComment.getCommentTime());
articleCommentUtil.setCommentator(articleComment.getCommentator()
.getUsername());
if (articleComment.getArticleComment() != null) {
articleCommentUtil.setReplyer(articleComment
.getArticleComment().getCommentator().getUsername());
}
jsonList.add(articleCommentUtil);
}
Json解析時出現net.sf.json.JSONException: There is a cycle in the hierarchy!