日期:2014-05-19  浏览次数:20766 次

GoogleGson转换复杂java对象,请求帮忙!急急急!!!!!!!!!!!
public class Article {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
@JoinColumn(name="acid") 
private ArticleClass ac;

@Column(length=80,nullable=false)
private String title;
}

public class ArticleClass{

@Id
  @GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@Column(length=36,nullable=false)
private String name;

@Column(length=200)
private String note;

@Column(nullable=false)
private Boolean visible=true;

@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")
private Set<ArticleClass> childs = new HashSet<ArticleClass>();


@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="parentid")
private ArticleClass parent;

@OneToMany(mappedBy="ac", cascade=CascadeType.REMOVE)
private Set<Article> articles = new HashSet<Article>();
}



List<Article> lists = getResultlist();
for(Article article : lists){ 
  System.out.println(article.getId);
  System.out.println(article.getAc().getName());
}//到这一步肯定是对的,能获取数据
//下面死活不行,出错,大致提示是反射出错.
Type type = new TypeToken<List<Article>>(){}.getType();
Json result = gson.toJson(lists,type);


出错,貌似出错应该跟article 实体跟articleclass有关系。总之解决问题就行,哪怕不用GoogleGson。用其他的。除了自己组拼字符串。

谁能帮忙解决一下,

------解决方案--------------------
咦,这个是啥?我用的GSON都没带这么复杂的,直接gson.toJson(Object obj,Class clazz)就行了