当我从User调用findById方法时,会收到结果,但是当我尝试转换Web服务的返回值时,会抛出此异常:
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.empsys.user.User["locations"]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location["contact"]->com.empsys.user.User["locations"]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location["contact"]->com.empsys.user.User["locations"]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location["contact"]->com.empsys.user.User["locations"]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location["contact"] ...
类关系为:
Contact类-创建该类以表示系统上的多种联系人。
@Entity
@Table(name ="CONTACT", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name ="CONTACT_TYPE")
public class Contact implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="contact")
@Column(nullable = true)
@JsonManagedReference
private List locations;
...
类User-创建该类是为了向用户表示特定信息
@Entity
@Table(name ="USER")
@DiscriminatorValue("USER")
public class User extends Contact {
...
类位置-创建该类以表示用户和系统其他联系人的地址
@Entity
@Table(name ="LOCATION", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Location implements Serializable {
@ManyToOne
@JoinColumn(name ="contact_id", nullable = false)
@JsonBackReference
private Contact contact;
...
使用的依赖项:
(com.sun.jersey)jersey-json:版本1.18
(com.fasterxml.jackson.datatype)jackson-datatype-hibernate4:版本:
2.4.0
有人可以帮我吗?
与Jackson JSON和Hibernate JPA问题进行的无限递归的可能重复项
我知道这是一篇很老的文章,但是对于那些将来要来这里的人来说,也许您应该看看这篇文章。
从Jackson 1.6开始,您可以使用@JsonManagedReference和@JsonBackReference。
我遇到了这样的问题。 在我的情况下,我在创建响应JSON时不希望导航的属性上使用@JsonIgnore。
我需要在此属性中导航,但是我已经尝试使用@JsonIgnore前进,但仍会发生异常。