4

When I call findById method from User, a result is received, however when I try to convert the return from webservice, this exception is thrown:

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"] ...

The classes relationship is:

Class Contact - This class was created to represents multiple types of contacts on system.

@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<Location> locations;
...

Class User - This class was created to represent Users with specific information

@Entity
@Table(name = "USER")
@DiscriminatorValue("USER")
public class User extends Contact {
...

Class Location - This class was created to represent addresses of users and others contacts of system

@Entity
@Table(name = "LOCATION", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Location implements Serializable {

    @ManyToOne
    @JoinColumn(name = "contact_id", nullable = false)
    @JsonBackReference
    private Contact contact;
    ...

Dependencies used:

  • (com.sun.jersey)jersey-json: version 1.18
  • (com.fasterxml.jackson.datatype)jackson-datatype-hibernate4: version: 2.4.0

Could anyone help me?

Jacob
  • 14,463
  • 65
  • 207
  • 320
  • possible duplicate of [Infinite Recursion with Jackson JSON and Hibernate JPA issue](http://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue) – fps Mar 19 '15 at 01:23

2 Answers2

3

I know this is a pretty old post but for those who get here in the future to come, maybe you should take a look at this post.

Since Jackson 1.6 you may use @JsonManagedReference and @JsonBackReference.

Community
  • 1
  • 1
Sidney de Moraes
  • 993
  • 3
  • 11
  • 29
0

I've had issues just like this. In my case I use @JsonIgnore on the properties I don't want navigated when creating the response JSON.

preston.m.price
  • 646
  • 1
  • 10
  • 17
  • 1
    I need to navigate in this property, and nevertheless I've tried to use @JsonIgnore to step forward but the exception still happens. – Marcelo Castro Mar 23 '15 at 12:14