1
我有自参照类自引用记录导致“直接自我引用导致循环”异常
@Entity
@Table(name = "contacts")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "contacts")
public class Contacts implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "password_smartlpc")
private String password;
@Column(name = "full_name")
private String fullName;
@ManyToOne
private Contacts companyContact;
}
但是对于我一个数据库记录
id full_name username password company_contact_id
5 JAK movies abc xyz 5
这个纪录company_contact_id为自我id.Which当检索进入自我参考周期。
Enter: com.fps.web.rest.errors.ExceptionTranslator.processRuntimeException()
with argument[s] =
[org.springframework.http.converter.HttpMessageNotWritableException: Could not
write content: Direct self-reference leading to cycle (through reference
chain: java.util.UnmodifiableRandomAccessList[2]-
>com.fps.domain.Contacts["companyContact"]-
>com.fps.domain.Contacts["companyContact"]); nested exception is
com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference
leading to cycle (through reference chain:
java.util.UnmodifiableRandomAccessList[2]-
>com.fps.domain.Contacts["companyContact"]-
>com.fps.domain.Contacts["companyContact"])]
解决我已经试过
(fetch = FetchType.LAZY) = gives same error as above.
@JsonIgnore : removes error but does not retrieves Company_Contact_id
@JsonManagedReference @JsonBackReference same as above.
不幸的是我不能改变数据库或者改变其it.Since更legacy.Any东西我可以尝试? 感谢
谢谢先生,这有助于很多:) –