5

the program under this particular environment: EJB3.0 + JPA + jersey Web Service

First Entity :

@Entity
@Table(name = "student_by_test_yao")

public class StudentTest implements Serializable {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;

    @ManyToOne
    @JoinColumn(name = "class_id")
    private ClassTest classes;

    public StudentTest() {}
}

Second Entity:

@Entity
@Table(name = "class_by_test_yao")
public class ClassTest implements Serializable{
    @Id
    @GeneratedValue
    private Integer id;
    private String name;

    @OneToMany(mappedBy = "classes",cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    private List<StudentTest> students;

    public ClassTest() {}
}

When I get the ClassTest 's students list. Exception is:

com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError)

If I change the fetch FetchType.LAZY the Exception is:

org.hibernate.LazyInitializationException: 
failed to lazily initialize a collection of role: 
cn.gomro.mid.core.biz.goods.test.ClassTest.students, 
could not initialize proxy - no Session

How to resolve my problems?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Adam Yao
  • 161
  • 2
  • 7
  • https://stackoverflow.com/questions/17542240/how-to-serialize-only-the-id-of-a-child-with-jackson This may help. The best way avoid loop. – sp518 Nov 19 '18 at 06:52
  • For anyone having this issue, best solution is: https://stackoverflow.com/a/18288939/6835976 – Olezt Mar 19 '19 at 13:49

4 Answers4

5
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId")
private User user;

it really worked. I just tried that on Bi-Directional @ManyToOne mapping. It fixed

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)

Guest
  • 51
  • 1
  • 1
3

Try to add @JsonIgnore annotation to one of fields to avoid looping

  • @JsonIgnore is useless. still in Excepton at 'org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:167) Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)' – Adam Yao Sep 01 '16 at 08:09
  • Hm, strange. It helped me when I had the same Exception – Vyacheslav Tsivina Sep 01 '16 at 08:13
1

For bidirectional relationships you can use these annotations:

@JsonManagedReference for the parent and @JsonBackReference for the child.

Also, this link might help: Jackson – Bidirectional Relationships

Alexander H
  • 198
  • 1
  • 8
  • Actually I need to get List in ClassTest also get Entity in StudentTest. – Adam Yao Sep 19 '16 at 09:13
  • I'm sorry, I'm not sure I got that.. You want to be able to fetch a StudentTest from a ClassTest and a ClassTest from a StudentTest? – Alexander H Sep 20 '16 at 11:03
  • 1
    Using \@JsonManagedReference and \@JsonBackReference you should be able to reconstruct both directions of the relationship during de-serialization. Of course in the JSON representation both sides of the relationship are not serialized, otherwise you would incur in the stack overflow since the entites would keep referencing each other. – Alexander H Sep 20 '16 at 11:52
  • @AlexanderH So I'm pretty sure what OP was trying to do is impossible _but_ what if I only wanted the `List>.length`? Perhaps it's possible to see how many relationships exist from the opposite side without causing an infinite recursion? – medley56 Feb 16 '18 at 21:33
  • I would define a `@JsonProperty` on the `ClassTest` side. Something like: `@JsonProperty public int classSize() { return classes.getStudents().size(); }` – Alexander H Feb 19 '18 at 12:54
1

Snippet must help you.

@JsonIgnore
@ManyToOne
@JoinColumn(name = "columnName", referencedColumnName = "id")
private Class class;
Syed Zain Ali
  • 1,206
  • 1
  • 14
  • 20