在尝试将具有双向关联的JPA对象转换为JSON时,我不断获取
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError)
Run Code Online (Sandbox Code Playgroud)
我找到的只是这个线程,基本上建议避免双向关联.有没有人对这个春天的bug有一个解决方法?
------编辑2010-07-24 16:26:22 -------
Codesnippets:
业务对象1:
@Entity
@Table(name = "ta_trainee", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Trainee extends BusinessObject {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "name", nullable = true)
private String name;
@Column(name = "surname", nullable = true)
private String surname;
@OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@Column(nullable = true)
private Set<BodyStat> bodyStats;
@OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, …Run Code Online (Sandbox Code Playgroud) 我正在使用RestEasy,Jboss 7和EJB 3.1.我正在创建一个以JSON格式返回数据的RESTful Web服务.
问题是我@ManyToOne在我的一个实体上有一个关系,它会在序列化过程中导致无限递归.我尝试使用杰克逊@JsonIgnore和@JsonBackReference注释来解决这个问题,但似乎它们被完全忽略了,无限递归仍在发生.
这是我的用户类:
class User {
private String userId;
private Role role;
@Id
@Column(name = "\"UserId\"", unique = true, nullable = false, length = 30)
public String getUserId() {
return this.UserId;
}
public void setUserId(String UserId) {
this.UserId = UserId;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "\"RoleId\"", nullable = false)
//I have tried @JsonIgnore without @JsonBackReference
@JsonIgnore
//I have tried @JsonBackReference without @JsonIgnore
//Also I have tried @JsonBackReference and @JsonIgnore together …Run Code Online (Sandbox Code Playgroud)