JPA @OneToOne stackoverflow

问题:

主表

@Data
@Entity(name = "husband")
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)

public class Husband extends BaseEntity {

....

@OneToOne(cascade = CascadeType.ALL, mappedBy = "husband", fetch = FetchType.LAZY)
private Wife wife;

}

从表

@Data
@Entity(name = "wife")
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)

public class Wife  extends BaseEntity {

....

@OneToOne
private Husband  husband ;

}

上述代码会出现内存溢出的问题,由于hashcode造成的相互调用形成死循环,可以修改任意一个表的@EqualsAndHashCode,如修改wife的可改为@EqualsAndHashCode(exclude = {"husband"}, callSuper = false)即可。

posted @ 2016-08-03 10:45  YH_W  阅读(448)  评论(0编辑  收藏  举报