Reputation: 27
I'm getting infinite error when selecting from a class that has multiple @OneToMany relationship, and i need your help with my mapping.
in fact i get orders with associated company and associated lineItems including products, so the JSON response is too long to show all the result.
I'v tried fetchType.EAGER and removing cascade at all but it doesn't seem to work.
Infinite Error
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.5.jar:2.9.5]
Mapping
@Entity
public class Contact implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@ManyToOne
@JoinColumn(name = "fk_company")
@JsonBackReference
private Company company;
}
@Entity
public class Company implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference
private Set<Contact> contacts = new HashSet<Contact>();
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JsonManagedReference
private Set<Order> orders = new HashSet<Order>();
}
@Entity
public class Order implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "fk_order")
private Set<LineItem> lineItems= new HashSet<LineItem>();
@ManyToOne
@JoinColumn(name = "fk_company")
@JsonBackReference
private Company company;
}
@Entity
public class LineItem implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@ManyToOne
@JoinColumn(name = "fk_product")
private Product product;
@ManyToOne
@JoinColumn(name = "fk_order")
@JsonBackReference
private Order order;
}
@Entity
public class Product implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
}
How can i fix that in my mapping and show all orders with associated Company, all companies, and all lineItems with associated Order and associated Product?
Upvotes: 0
Views: 674
Reputation: 1077
When the relation among the entities form loop, Spring serializer will have problem to generate json.
Try to introduce @JsonIgnore
on some fields to stop the loop.
Upvotes: 3