Reputation: 4156
I'm having problems with Jackson 1.6.3 and Hibernate. I found this thread here : Infinite Recursion with Jackson JSON and Hibernate JPA issue
But it did not solve the problem.
I have a Node object that has incoming and outgoing relations. Even with the @JsonManagedReference annotations, on the console of the app server I can see the exceptions being thrown (infinite recursion).
Is there any alternative to that?
@Entity
@Table(name="nodes")
public class Node implements Serializable {
@Id
private String id;
@Column(name="x_pos")
private double x;
@Column(name="y_pos")
private double y;
@OneToMany
@JoinColumn(name="source")
@JsonManagedReference("outgoingRelations")
private Set<Relation> outgoingRelations;
@OneToMany
@JoinColumn(name="target")
@JsonManagedReference("incomingRelations")
private Set<Relation> incomingRelations;
@Entity
@Table(name="relations")
public class Relation implements Serializable {
@Id
private Long id;
@ManyToOne
@JoinColumn(name="source")
@JsonBackReference("outgoingRelations")
private Node source;
@ManyToOne
@JoinColumn(name="target")
@JsonBackReference("incomingRelations")
private Node target;
Regards
Upvotes: 1
Views: 4225
Reputation: 7622
We can try to break the loop either at Node end or at Relation by following 3 ways
Use @JsonIdentityInfo
@Entity
@Table(name = "nodes")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Node {
...
}
@Entity
@Table(name = "relations")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Relation {
...
}
Use @JsonIgnore
@OneToMany(fetch = FetchType.LAZY,mappedBy="node")
@JsonIgnore
private List<Node> lstNode;
Refer more in detail here with the working demo as well.
Upvotes: 0
Reputation: 4184
From spring - Infinite Recursion with Jackson JSON and Hibernate JPA issue:
You may use @JsonIgnore to break the cycle.
Upvotes: 2