Springboot jpa 一对多保存实体时报错无限循环

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="crmUserId")
private CrmUser crmUser;

报错信息

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.swj.crm.entity.topline.CompanyUser["crmUser"]->com.swj.crm.entity.topline.CrmUser["companyUsers"]->org.hibernate.collection.internal.PersistentBag[0]->com.swj.crm.entity.topline.CompanyUser["crmUser"]->com.swj.crm.entity.topline.CrmUser["companyUsers"]->org.hibernate.collection.internal.PersistentBag[0]-
....

解决方法

@JsonBackReference
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="crmUserId")
private CrmUser crmUser;

你可能感兴趣的:(SpringBoot)