一类中有多个@OneToMany @ManyToOne双向关系

时间:2018-12-20 22:14:57

标签: java hibernate spring-data-jpa

从具有多个@OneToMany关系的类中进行选择时,出现无限错误,并且需要您的映射帮助。

事实上,我获得了与关联公司和包括产品在内的关联lineItem的订单,因此JSON响应太长,无法显示所有结果。

我尝试了fetchType.EAGER并完全删除了层叠,但似乎不起作用。

无限错误

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]

映射

@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)   
}

如何在映射中解决此问题,并显示与关联的公司,所有公司的所有订单以及与关联的订单和关联的产品的所有lineItem?

1 个答案:

答案 0 :(得分:0)

当实体之间的关系形成循环时,Spring序列化器将无法生成json。 尝试在某些字段上引入@JsonIgnore以停止循环。